-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from seleb/ts
Add generated typescript definitions as part of build
- Loading branch information
Showing
4 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,4 @@ typings/ | |
# Generated files | ||
services/rest.js | ||
flickr-sdk.js | ||
flickr-sdk.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<%_ function printMethods(map, prefix){ _%> | ||
<%_ Object.entries(map).forEach(([key, value]) => { _%> | ||
<%_ if (typeof value === 'string') { _%> | ||
<%= prefix %>/** [Flickr docs](<%= getDocsURL(value) %>) - Permissions: `<%= getPerms(value) %>` */ | ||
<%= prefix %><%= key _%>(options: { | ||
<%_ getArguments(value).filter(({ name }) => name !== 'api_key').forEach(({ name, optional }) => { _%> | ||
<%= prefix %> <%= name %><%= parseInt(optional, 10) ? '?' : '' %>: string | number; | ||
<%_ }); _%> | ||
<%= prefix %>}): FlickrResponse; | ||
<%_ } else { _%> | ||
<%= prefix %><%= key _%>: { | ||
<%= printMethods(value, `${prefix}\t`) _%> | ||
<%= prefix %>}; | ||
<%_ } _%> | ||
<%_ }); _%> | ||
<%_ } _%> | ||
|
||
// not comprehensive | ||
type FlickrResponse = Promise<{ | ||
body: any; | ||
ok: boolean; | ||
}>; | ||
|
||
declare class Flickr { | ||
constructor(auth: string); | ||
<%= printMethods(methodTree, '\t') _%> | ||
|
||
// TODO: define static classes | ||
OAuth: any; | ||
Feeds: any; | ||
Upload: any; | ||
Replace: any; | ||
} | ||
|
||
export = Flickr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
var ejs = require('ejs'); | ||
|
||
/** | ||
* Render the client file | ||
*/ | ||
|
||
ejs.renderFile(__dirname + '/build-types.ejs', { | ||
|
||
/** | ||
* All available method info responses | ||
* @type {Object} | ||
*/ | ||
|
||
methods: require('require-dir')('../src'), | ||
|
||
get methodTree() { | ||
return Object.keys(this.methods) | ||
.reduce((result, i) => { | ||
let parent = result; | ||
const keys = i.split('.').slice(1); | ||
const method = keys.pop(); | ||
|
||
for (let key of keys) { | ||
parent = parent[key] = parent[key] || {}; | ||
} | ||
parent[method] = i; | ||
return result; | ||
}, {}); | ||
}, | ||
|
||
/** | ||
* Returns all of the arguments for `method`. | ||
* @param {String} method | ||
* @returns {String[]} | ||
*/ | ||
|
||
getArguments: function (method) { | ||
return this.methods[method] | ||
.arguments | ||
.argument; | ||
}, | ||
|
||
/** | ||
* Returns the level of perms required for this method. | ||
* @param {String} method | ||
* @returns {String} | ||
*/ | ||
|
||
getPerms: function (method) { | ||
switch (parseInt(this.methods[method].method.requiredperms, 10)) { | ||
case 1: | ||
return 'read'; | ||
case 2: | ||
return 'write'; | ||
case 3: | ||
return 'delete'; | ||
default: | ||
return 'none'; | ||
} | ||
}, | ||
|
||
/** | ||
* Returns the Flickr API documentation url for `method`. | ||
* @param {String} method | ||
* @returns {String} | ||
*/ | ||
|
||
getDocsURL: function (method) { | ||
return 'https://www.flickr.com/services/api/' + method + '.html'; | ||
} | ||
|
||
}, function (err, str) { | ||
if (err) { | ||
throw err; | ||
} | ||
process.stdout.write(str); | ||
}); |