Skip to content

Commit 915afbb

Browse files
committedAug 20, 2020
Some type improvements
1 parent 63aafdf commit 915afbb

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed
 

‎index.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const defaultUserAgent = pkg.name + '/' + pkg.version + (pkg.homepage ? ' (' + p
2020
/** @typedef {import('querystring').ParsedUrlQuery} ParsedUrlQuery */
2121
/** @typedef {import('express').Request} Request */
2222
/** @typedef {import('express').Response} Response */
23-
/** @typedef {import('express').Router} Router */
2423

2524
// TODO: Figure out how to import this definition from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/03fddd7a3f2322433a867d9edcee561ac85d950d/types/multer/index.d.ts#L103-L124
2625
/** @typedef {*} MulterFile */
@@ -43,11 +42,11 @@ const defaultUserAgent = pkg.name + '/' + pkg.version + (pkg.homepage ? ' (' + p
4342
/**
4443
* @typedef MinimalParsedMicropubStructure
4544
* @property {string[]|undefined} [type]
46-
* @property {Object<string,any[]>} properties
47-
* @property {Object<string,any[]>} mp
45+
* @property {{ [property: string]: import('type-fest').JsonValue[]}} properties
46+
* @property {{ [property: string]: import('type-fest').JsonValue[]}} mp
4847
*/
4948

50-
/** @typedef {MinimalParsedMicropubStructure & { [reservedPropertyName: string]: any }} ParsedMicropubStructure */
49+
/** @typedef {MinimalParsedMicropubStructure & import('type-fest').JsonObject} ParsedMicropubStructure */
5150

5251
/**
5352
* @template T
@@ -298,22 +297,30 @@ const processFiles = function (body, files, logger) {
298297
/** @typedef {TokenReferenceResolver|MaybeArray<TokenReference>} TokenReferenceOption */
299298

300299
/**
301-
* @param {Object} options
302-
* @param {(data: ParsedMicropubStructure, req: Request) => (undefined|{url: string})} options.handler
303-
* @param {TokenReferenceOption} options.tokenReference
304-
* @param {BunyanLite} [options.logger]
305-
* @param {(q: string, req: Request) => any} [options.queryHandler]
306-
* @param {string} [options.userAgent]
307-
* @returns {Router}
300+
* @typedef MicropubExpressOptions
301+
* @property {(data: ParsedMicropubStructure, req: Request) => (undefined|{url: string})} handler
302+
* @property {TokenReferenceOption} tokenReference
303+
* @property {BunyanLite} [logger]
304+
* @property {(q: string, req: Request) => any} [queryHandler]
305+
* @property {string} [userAgent]
306+
*/
307+
308+
/**
309+
* @param {MicropubExpressOptions} options
310+
* @returns {import('express').Router}
308311
*/
309312
const micropubExpress = function (options) {
310-
const logger = options.logger || getBunyanAdaptor();
313+
const {
314+
logger = getBunyanAdaptor(),
315+
handler,
316+
queryHandler
317+
} = options;
311318

312319
if (!options.tokenReference || !['function', 'object'].includes(typeof options.tokenReference)) {
313320
throw new Error('No correct token set. It\'s needed for authorization checks.');
314321
}
315322

316-
if (!options.handler || typeof options.handler !== 'function') {
323+
if (!handler || typeof handler !== 'function') {
317324
throw new Error('No correct handler set. It\'s needed to actually process a Micropub request.');
318325
}
319326

@@ -497,14 +504,14 @@ const micropubExpress = function (options) {
497504
return badRequest(res, 'Invalid q parameter format');
498505
}
499506

500-
if (!options.queryHandler) {
507+
if (!queryHandler) {
501508
return query === 'config' ? res.json({}) : badRequest(res, 'Queries are not supported');
502509
}
503510

504511
// Not using "await" here as the middleware shouldn't be returning a Promise, as Express doesn't understand Promises natively yet and it could hide exceptions thrown
505512
Promise.resolve()
506513
.then(async () => {
507-
const result = await options.queryHandler(query, req);
514+
const result = await queryHandler(query, req);
508515

509516
if (!result) {
510517
return query === 'config' ? res.json({}) : badRequest(res, 'Query type is not supported');
@@ -544,7 +551,7 @@ const micropubExpress = function (options) {
544551
// Not using "await" here as the middleware shouldn't be returning a Promise, as Express doesn't understand Promises natively yet and it could hide exceptions thrown
545552
Promise.resolve()
546553
.then(async () => {
547-
const result = await options.handler(data, req);
554+
const result = await handler(data, req);
548555

549556
if (!result || !result.url) {
550557
return res.sendStatus(400);

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"sinon": "^9.0.3",
7474
"sinon-chai": "^3.5.0",
7575
"supertest": "4.0.2",
76-
"typescript": "^3.9.7"
76+
"type-fest": "^0.16.0",
77+
"typescript": "^4.0.2"
7778
},
7879
"dependencies": {
7980
"body-parser": "^1.13.1",

‎test/integration/micropub.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('Micropub API', function () {
100100
};
101101

102102
/**
103-
* @param {NockScope} [mock]
103+
* @param {NockScope|undefined} mock
104104
* @param {MochaDone} done
105105
* @param {number} [code]
106106
* @param {*} [content]

0 commit comments

Comments
 (0)
Please sign in to comment.