From f26696ad1980206be1efc54f1f407b067801a5a7 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 26 Nov 2018 15:32:07 +0100 Subject: [PATCH 1/3] add typings for open-apifactory --- types/openapi-factory/index.d.ts | 49 +++++++++++++++++++ .../openapi-factory/openapi-factory-tests.ts | 25 ++++++++++ types/openapi-factory/tsconfig.json | 23 +++++++++ types/openapi-factory/tslint.json | 3 ++ 4 files changed, 100 insertions(+) create mode 100644 types/openapi-factory/index.d.ts create mode 100644 types/openapi-factory/openapi-factory-tests.ts create mode 100644 types/openapi-factory/tsconfig.json create mode 100644 types/openapi-factory/tslint.json diff --git a/types/openapi-factory/index.d.ts b/types/openapi-factory/index.d.ts new file mode 100644 index 0000000000..6cb22f7194 --- /dev/null +++ b/types/openapi-factory/index.d.ts @@ -0,0 +1,49 @@ +// Type definitions for openapi-factory 4.2 +// Project: https://github.com/wparad/openapi-factory.js +// Definitions by: Daan Boerlage +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +declare namespace OpenApi { + interface OpenApiOptions { + requestMiddleware?: () => any; + responseMiddleware?: () => any; + errorMiddleware?: () => any; + } +} + +declare class OpenApi { + constructor(options: OpenApi.OpenApiOptions, overrideLogger?: () => void); + + setAuthorizer(authorizerFunc: (req?: any) => any): void; + onEvent(onEventFunc: (req?: any) => any): void; + onSchedule(onScheduleFunc: (req?: any) => any): void; + + head(route: string, p0: (req?: any) => any): void; + head(route: string, p0: object, p1: (req?: any) => any): void; + + get(route: string, p0: (req?: any) => any): void; + get(route: string, p0: object, p1: (req?: any) => any): void; + + post(route: string, p0: (req?: any) => any): void; + post(route: string, p0: object, p1: (req?: any) => any): void; + + put(route: string, p0: (req?: any) => any): void; + put(route: string, p0: object, p1: (req?: any) => any): void; + + patch(route: string, p0: (req?: any) => any): void; + patch(route: string, p0: object, p1: (req?: any) => any): void; + + delete(route: string, p0: (req?: any) => any): void; + delete(route: string, p0: object, p1: (req?: any) => any): void; + + options(route: string, p0: (req?: any) => any): void; + options(route: string, p0: object, p1: (req?: any) => any): void; + + any(route: string, p0: (req?: any) => any): void; + any(route: string, p0: object, p1: (req?: any) => any): void; + + handler(event: object, context: object): Promise; +} + +export = OpenApi; diff --git a/types/openapi-factory/openapi-factory-tests.ts b/types/openapi-factory/openapi-factory-tests.ts new file mode 100644 index 0000000000..13e1e0e7cc --- /dev/null +++ b/types/openapi-factory/openapi-factory-tests.ts @@ -0,0 +1,25 @@ +import * as OpenApiFactory from 'openapi-factory'; + +const api = new OpenApiFactory({}); + +api.setAuthorizer((req: any) => req); +api.onEvent((req: any) => req); +api.onSchedule((req: any) => req); + +api.head('/v1/test', () => 'success'); +api.get('/v1/test', () => 'success'); +api.post('/v1/test', () => 'success'); +api.put('/v1/test', () => 'success'); +api.patch('/v1/test', () => 'success'); +api.delete('/v1/test', () => 'success'); +api.options('/v1/test', () => 'success'); +api.any('/v1/test2', () => 'success'); + +api.head('/v2/test', {test: true}, () => 'success'); +api.get('/v2/test', {test: true}, () => 'success'); +api.post('/v2/test', {test: true}, () => 'success'); +api.put('/v2/test', {test: true}, () => 'success'); +api.patch('/v2/test', {test: true}, () => 'success'); +api.delete('/v2/test', {test: true}, () => 'success'); +api.options('/v2/test', {test: true}, () => 'success'); +api.any('/v2/test2', {test: true}, () => 'success'); diff --git a/types/openapi-factory/tsconfig.json b/types/openapi-factory/tsconfig.json new file mode 100644 index 0000000000..e8528e8e84 --- /dev/null +++ b/types/openapi-factory/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "openapi-factory-tests.ts" + ] +} diff --git a/types/openapi-factory/tslint.json b/types/openapi-factory/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/openapi-factory/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} From 7bba952b5759fceb1b6307c807482380600047b5 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 26 Nov 2018 18:29:53 +0100 Subject: [PATCH 2/3] [openapi-factory] fix http methods param typings --- types/openapi-factory/index.d.ts | 52 +++++++++++-------- .../openapi-factory/openapi-factory-tests.ts | 36 +++++++------ 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/types/openapi-factory/index.d.ts b/types/openapi-factory/index.d.ts index 6cb22f7194..e41958e6df 100644 --- a/types/openapi-factory/index.d.ts +++ b/types/openapi-factory/index.d.ts @@ -5,43 +5,53 @@ // TypeScript Version: 2.2 declare namespace OpenApi { - interface OpenApiOptions { + interface ApiOptions { requestMiddleware?: () => any; responseMiddleware?: () => any; errorMiddleware?: () => any; } + + interface HttpMethodOptions { + rawBody?: boolean; + } + + interface HttpResponse { + statusCode?: number; + headers?: object; + body?: (object | string); + } } declare class OpenApi { - constructor(options: OpenApi.OpenApiOptions, overrideLogger?: () => void); + constructor(options: OpenApi.ApiOptions, overrideLogger?: () => void); - setAuthorizer(authorizerFunc: (req?: any) => any): void; - onEvent(onEventFunc: (req?: any) => any): void; - onSchedule(onScheduleFunc: (req?: any) => any): void; + setAuthorizer(authorizerFunc: (req?: any) => Promise): void; + onEvent(onEventFunc: (req?: any) => Promise): void; + onSchedule(onScheduleFunc: (req?: any) => Promise): void; - head(route: string, p0: (req?: any) => any): void; - head(route: string, p0: object, p1: (req?: any) => any): void; + head(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + head(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: any) => any): void; - get(route: string, p0: (req?: any) => any): void; - get(route: string, p0: object, p1: (req?: any) => any): void; + get(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + get(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; - post(route: string, p0: (req?: any) => any): void; - post(route: string, p0: object, p1: (req?: any) => any): void; + post(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + post(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; - put(route: string, p0: (req?: any) => any): void; - put(route: string, p0: object, p1: (req?: any) => any): void; + put(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + put(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; - patch(route: string, p0: (req?: any) => any): void; - patch(route: string, p0: object, p1: (req?: any) => any): void; + patch(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + patch(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; - delete(route: string, p0: (req?: any) => any): void; - delete(route: string, p0: object, p1: (req?: any) => any): void; + delete(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + delete(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; - options(route: string, p0: (req?: any) => any): void; - options(route: string, p0: object, p1: (req?: any) => any): void; + options(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + options(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; - any(route: string, p0: (req?: any) => any): void; - any(route: string, p0: object, p1: (req?: any) => any): void; + any(route: string, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; + any(route: string, options: OpenApi.HttpMethodOptions, handler: (req?: object) => (OpenApi.HttpResponse | Promise)): void; handler(event: object, context: object): Promise; } diff --git a/types/openapi-factory/openapi-factory-tests.ts b/types/openapi-factory/openapi-factory-tests.ts index 13e1e0e7cc..ad24a5021d 100644 --- a/types/openapi-factory/openapi-factory-tests.ts +++ b/types/openapi-factory/openapi-factory-tests.ts @@ -6,20 +6,24 @@ api.setAuthorizer((req: any) => req); api.onEvent((req: any) => req); api.onSchedule((req: any) => req); -api.head('/v1/test', () => 'success'); -api.get('/v1/test', () => 'success'); -api.post('/v1/test', () => 'success'); -api.put('/v1/test', () => 'success'); -api.patch('/v1/test', () => 'success'); -api.delete('/v1/test', () => 'success'); -api.options('/v1/test', () => 'success'); -api.any('/v1/test2', () => 'success'); +const testReponse = { + statusCode: 200 +}; -api.head('/v2/test', {test: true}, () => 'success'); -api.get('/v2/test', {test: true}, () => 'success'); -api.post('/v2/test', {test: true}, () => 'success'); -api.put('/v2/test', {test: true}, () => 'success'); -api.patch('/v2/test', {test: true}, () => 'success'); -api.delete('/v2/test', {test: true}, () => 'success'); -api.options('/v2/test', {test: true}, () => 'success'); -api.any('/v2/test2', {test: true}, () => 'success'); +api.head('/v1/test', () => testReponse); +api.get('/v1/test', () => testReponse); +api.post('/v1/test', () => testReponse); +api.put('/v1/test', () => testReponse); +api.patch('/v1/test', () => testReponse); +api.delete('/v1/test', () => testReponse); +api.options('/v1/test', () => testReponse); +api.any('/v1/test2', () => testReponse); + +api.head('/v2/test', { rawBody: true }, () => testReponse); +api.get('/v2/test', { rawBody: true }, () => testReponse); +api.post('/v2/test', { rawBody: true }, () => testReponse); +api.put('/v2/test', { rawBody: true }, () => testReponse); +api.patch('/v2/test', { rawBody: true }, () => testReponse); +api.delete('/v2/test', { rawBody: true }, () => testReponse); +api.options('/v2/test', { rawBody: true }, () => testReponse); +api.any('/v2/test2', { rawBody: true }, () => testReponse); From ec2874d4b8d43ed49da56ff303660fa242b614e2 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Dec 2018 01:19:16 +0100 Subject: [PATCH 3/3] [openapi-factory] set strictFunctionTypes to true and change import in example code --- types/openapi-factory/openapi-factory-tests.ts | 2 +- types/openapi-factory/tsconfig.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/openapi-factory/openapi-factory-tests.ts b/types/openapi-factory/openapi-factory-tests.ts index ad24a5021d..8213104a44 100644 --- a/types/openapi-factory/openapi-factory-tests.ts +++ b/types/openapi-factory/openapi-factory-tests.ts @@ -1,4 +1,4 @@ -import * as OpenApiFactory from 'openapi-factory'; +import OpenApiFactory = require('openapi-factory'); const api = new OpenApiFactory({}); diff --git a/types/openapi-factory/tsconfig.json b/types/openapi-factory/tsconfig.json index e8528e8e84..e9035817a2 100644 --- a/types/openapi-factory/tsconfig.json +++ b/types/openapi-factory/tsconfig.json @@ -7,7 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, - "strictFunctionTypes": false, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../"