Skip to content

Commit

Permalink
Merge pull request #141 from seleb/ts
Browse files Browse the repository at this point in the history
Add generated typescript definitions as part of build
  • Loading branch information
jeremyruppel authored Apr 26, 2023
2 parents 514d783 + dc47df2 commit b63431d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ typings/
# Generated files
services/rest.js
flickr-sdk.js
flickr-sdk.d.ts
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"search"
],
"main": "index.js",
"types": "flickr-sdk.d.ts",
"files": [
"index.js",
"flickr-sdk.d.ts",
"flickr-sdk.js",
"lib",
"plugins",
Expand All @@ -29,8 +31,9 @@
"build-rest": "node script/build-rest > services/rest.js",
"build-tests": "node script/build-tests",
"build-docs": "node script/build-docs > README.md",
"build-types": "node script/build-types > flickr-sdk.d.ts",
"build-client": "browserify -s Flickr $npm_package_main > flickr-sdk.js",
"build": "npm run build-rest && npm run build-docs && npm run build-client",
"build": "npm run build-rest && npm run build-docs && npm run build-types && npm run build-client",
"lint": "eslint .",
"test": "mocha",
"coverage": "nyc mocha",
Expand Down
35 changes: 35 additions & 0 deletions script/build-types.ejs
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;
77 changes: 77 additions & 0 deletions script/build-types.js
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);
});

0 comments on commit b63431d

Please sign in to comment.