@smartlyio/oats-koa-adapter 3.3.1 → 4.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -3
- package/README.template.md +10 -3
- package/dist/index.d.ts +12 -4
- package/dist/index.js +13 -8
- package/dist/index.js.map +1 -1
- package/index.ts +68 -43
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Oats Koa Adapter is a library that binds server endpoints written with [Oats](ht
|
|
|
13
13
|
Use `npm` or `yarn` to install `oats-koa-adapter`.
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
npm install oats-koa-adapter
|
|
16
|
+
npm install @smartlyio/oats-koa-adapter
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
@@ -33,8 +33,15 @@ import spec from '<Your Route Definitions>';
|
|
|
33
33
|
export const router = () => {
|
|
34
34
|
const requestContextCreator = (ctx: any): any => ctx;
|
|
35
35
|
|
|
36
|
-
const
|
|
37
|
-
|
|
36
|
+
const middlewares = [(_ctx, next) => { return next() }];
|
|
37
|
+
|
|
38
|
+
const oatsRouter = koaAdapter.bind({
|
|
39
|
+
handler: oaserver.createRouter(),
|
|
40
|
+
spec,
|
|
41
|
+
requestContextCreator,
|
|
42
|
+
middlewares
|
|
43
|
+
});
|
|
44
|
+
|
|
38
45
|
return new Router().use(oatsRouter.routes())
|
|
39
46
|
};
|
|
40
47
|
|
package/README.template.md
CHANGED
|
@@ -13,7 +13,7 @@ Oats Koa Adapter is a library that binds server endpoints written with [Oats](ht
|
|
|
13
13
|
Use `npm` or `yarn` to install `oats-koa-adapter`.
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
npm install oats-koa-adapter
|
|
16
|
+
npm install @smartlyio/oats-koa-adapter
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
@@ -33,8 +33,15 @@ import spec from '<Your Route Definitions>';
|
|
|
33
33
|
export const router = () => {
|
|
34
34
|
const requestContextCreator = (ctx: any): any => ctx;
|
|
35
35
|
|
|
36
|
-
const
|
|
37
|
-
|
|
36
|
+
const middlewares = [(_ctx, next) => { return next() }];
|
|
37
|
+
|
|
38
|
+
const oatsRouter = koaAdapter.bind({
|
|
39
|
+
handler: oaserver.createRouter(),
|
|
40
|
+
spec,
|
|
41
|
+
requestContextCreator,
|
|
42
|
+
middlewares
|
|
43
|
+
});
|
|
44
|
+
|
|
38
45
|
return new Router().use(oatsRouter.routes())
|
|
39
46
|
};
|
|
40
47
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
import * as Router from 'koa-router';
|
|
2
2
|
import { ParameterizedContext } from 'koa';
|
|
3
3
|
import * as runtime from '@smartlyio/oats-runtime';
|
|
4
|
+
import { IMiddleware } from 'koa-router';
|
|
5
|
+
declare type Opts<Spec, StateT, CustomT, RequestContext> = {
|
|
6
|
+
handler: runtime.server.HandlerFactory<Spec>;
|
|
7
|
+
spec: Spec;
|
|
8
|
+
requestContextCreator?: (ctx: ParameterizedContext<StateT, CustomT>) => RequestContext;
|
|
9
|
+
middlewares?: Array<IMiddleware<StateT, CustomT>>;
|
|
10
|
+
};
|
|
4
11
|
/**
|
|
5
12
|
* Bind provided handlers for the OpenAPI routes
|
|
6
13
|
*
|
|
7
14
|
* Koa's default StateT and CustomT values are selected as defaults
|
|
8
|
-
* @param
|
|
9
|
-
* @param spec
|
|
10
|
-
* @param requestContextCreator
|
|
15
|
+
* @param opts Named arguments to the function
|
|
16
|
+
* @param spec Deprecated use the named arguments
|
|
17
|
+
* @param requestContextCreator Deprecated
|
|
11
18
|
*/
|
|
12
|
-
export declare function bind<Spec, RequestContext = void, StateT = any, CustomT = Record<string, unknown>>(
|
|
19
|
+
export declare function bind<Spec, RequestContext = void, StateT = any, CustomT = Record<string, unknown>>(opts: runtime.server.HandlerFactory<Spec> | Opts<Spec, StateT, CustomT, RequestContext>, spec?: Spec, requestContextCreator?: (ctx: ParameterizedContext<StateT, CustomT>) => RequestContext): Router<StateT, CustomT>;
|
|
20
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.bind = void 0;
|
|
4
4
|
const Router = require("koa-router");
|
|
5
5
|
const runtime = require("@smartlyio/oats-runtime");
|
|
6
|
-
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
function adapter(router, requestContextCreator, middlewares) {
|
|
7
8
|
return (path, op, method, handler) => {
|
|
8
9
|
const koaPath = path.replace(/{([^}]+)}/g, (m, param) => ':' + param);
|
|
9
|
-
router[method](koaPath, async (ctx) => {
|
|
10
|
+
router[method](koaPath, ...middlewares, async (ctx) => {
|
|
10
11
|
const files = ctx.request.files;
|
|
11
12
|
let fileFields = {};
|
|
12
13
|
if (files) {
|
|
@@ -39,8 +40,8 @@ function adapter(router, requestContextCreator) {
|
|
|
39
40
|
body,
|
|
40
41
|
requestContext: requestContextCreator(ctx)
|
|
41
42
|
});
|
|
42
|
-
ctx.status = result.status;
|
|
43
43
|
ctx.body = result.value.value;
|
|
44
|
+
ctx.status = result.status;
|
|
44
45
|
ctx.set(result.headers);
|
|
45
46
|
});
|
|
46
47
|
};
|
|
@@ -49,13 +50,17 @@ function adapter(router, requestContextCreator) {
|
|
|
49
50
|
* Bind provided handlers for the OpenAPI routes
|
|
50
51
|
*
|
|
51
52
|
* Koa's default StateT and CustomT values are selected as defaults
|
|
52
|
-
* @param
|
|
53
|
-
* @param spec
|
|
54
|
-
* @param requestContextCreator
|
|
53
|
+
* @param opts Named arguments to the function
|
|
54
|
+
* @param spec Deprecated use the named arguments
|
|
55
|
+
* @param requestContextCreator Deprecated
|
|
55
56
|
*/
|
|
56
|
-
function bind(
|
|
57
|
+
function bind(opts, spec, requestContextCreator) {
|
|
58
|
+
const arg = typeof opts === 'function'
|
|
59
|
+
? { handler: opts, spec, requestContextCreator, middlewares: [] }
|
|
60
|
+
: opts;
|
|
61
|
+
assert(arg.spec, 'Missing spec argument -- please use the named argument version of bind');
|
|
57
62
|
const router = new Router();
|
|
58
|
-
handler(adapter(router, requestContextCreator || (() => ({}))))(spec);
|
|
63
|
+
arg.handler(adapter(router, arg.requestContextCreator || (() => ({})), arg.middlewares || []))(arg.spec);
|
|
59
64
|
return router;
|
|
60
65
|
}
|
|
61
66
|
exports.bind = bind;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,qCAAqC;AAErC,mDAAmD;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,qCAAqC;AAErC,mDAAmD;AACnD,iCAAiC;AAGjC,SAAS,OAAO,CACd,MAA+B,EAC/B,qBAAqF,EACrF,WAAgD;IAEhD,OAAO,CACL,IAAY,EACZ,EAAU,EACV,MAA8B,EAC9B,OAAoC,EACpC,EAAE;QACF,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC;QACrE,MAAc,CAAC,MAAM,CAAC,CACrB,OAAO,EACP,GAAG,WAAW,EACd,KAAK,EAAE,GAA0C,EAAE,EAAE;YACnD,MAAM,KAAK,GAAI,GAAW,CAAC,OAAO,CAAC,KAAK,CAAC;YACzC,IAAI,UAAU,GAAG,EAAE,CAAC;YACpB,IAAI,KAAK,EAAE;gBACT,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,IAAS,EAAE,IAAI,EAAE,EAAE;oBACzD,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAChC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAChB,KAAK,CA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,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1B,CAAC,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AASD;;;;;;;GAOG;AACH,SAAgB,IAAI,CAClB,IAAuF,EACvF,IAAW,EACX,qBAAsF;IAEtF,MAAM,GAAG,GACP,OAAO,IAAI,KAAK,UAAU;QACxB,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,qBAAqB,EAAE,WAAW,EAAE,EAAE,EAAE;QACjE,CAAC,CAAC,IAAI,CAAC;IACX,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,wEAAwE,CAAC,CAAC;IAC3F,MAAM,MAAM,GAAG,IAAI,MAAM,EAAmB,CAAC;IAC7C,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,qBAAqB,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,CAC5F,GAAG,CAAC,IAAI,CACT,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AAfD,oBAeC"}
|
package/index.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import * as Router from 'koa-router';
|
|
2
2
|
import { ParameterizedContext } from 'koa';
|
|
3
3
|
import * as runtime from '@smartlyio/oats-runtime';
|
|
4
|
+
import * as assert from 'assert';
|
|
5
|
+
import { IMiddleware } from 'koa-router';
|
|
4
6
|
|
|
5
7
|
function adapter<StateT, CustomT, RequestContext>(
|
|
6
8
|
router: Router<StateT, CustomT>,
|
|
7
|
-
requestContextCreator: (ctx: ParameterizedContext<StateT, CustomT>) => RequestContext
|
|
9
|
+
requestContextCreator: (ctx: ParameterizedContext<StateT, CustomT>) => RequestContext,
|
|
10
|
+
middlewares: Array<IMiddleware<StateT, CustomT>>
|
|
8
11
|
): runtime.server.ServerAdapter {
|
|
9
12
|
return (
|
|
10
13
|
path: string,
|
|
@@ -13,61 +16,83 @@ function adapter<StateT, CustomT, RequestContext>(
|
|
|
13
16
|
handler: runtime.server.SafeEndpoint
|
|
14
17
|
) => {
|
|
15
18
|
const koaPath = path.replace(/{([^}]+)}/g, (m, param) => ':' + param);
|
|
16
|
-
(router as any)[method](
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
(router as any)[method](
|
|
20
|
+
koaPath,
|
|
21
|
+
...middlewares,
|
|
22
|
+
async (ctx: ParameterizedContext<StateT, CustomT>) => {
|
|
23
|
+
const files = (ctx as any).request.files;
|
|
24
|
+
let fileFields = {};
|
|
25
|
+
if (files) {
|
|
26
|
+
fileFields = Object.keys(files).reduce((memo: any, name) => {
|
|
27
|
+
memo[name] = new runtime.make.File(
|
|
28
|
+
files[name].path,
|
|
29
|
+
files[name].size,
|
|
30
|
+
files[name].name
|
|
31
|
+
);
|
|
32
|
+
return memo;
|
|
33
|
+
}, {});
|
|
34
|
+
}
|
|
35
|
+
const contentType = ctx.request.type;
|
|
36
|
+
const requestBody = (ctx.request as any).body;
|
|
37
|
+
let body;
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
if (Array.isArray(requestBody)) {
|
|
40
|
+
body = { value: requestBody, contentType };
|
|
41
|
+
} else if (typeof requestBody === 'object') {
|
|
42
|
+
const bodyWithFileFields = { ...requestBody, ...fileFields };
|
|
43
|
+
body =
|
|
44
|
+
Object.keys(bodyWithFileFields).length > 0
|
|
45
|
+
? { value: bodyWithFileFields, contentType }
|
|
46
|
+
: undefined;
|
|
47
|
+
}
|
|
38
48
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
const result = await handler({
|
|
50
|
+
path,
|
|
51
|
+
method: runtime.server.assertMethod(ctx.method.toLowerCase()),
|
|
52
|
+
servers: [],
|
|
53
|
+
op,
|
|
54
|
+
headers: ctx.request.headers,
|
|
55
|
+
params: (ctx as any).params,
|
|
56
|
+
query: ctx.query,
|
|
57
|
+
body,
|
|
58
|
+
requestContext: requestContextCreator(ctx)
|
|
59
|
+
});
|
|
60
|
+
ctx.body = result.value.value;
|
|
61
|
+
ctx.status = result.status;
|
|
62
|
+
ctx.set(result.headers);
|
|
63
|
+
}
|
|
64
|
+
);
|
|
54
65
|
};
|
|
55
66
|
}
|
|
56
67
|
|
|
68
|
+
type Opts<Spec, StateT, CustomT, RequestContext> = {
|
|
69
|
+
handler: runtime.server.HandlerFactory<Spec>;
|
|
70
|
+
spec: Spec;
|
|
71
|
+
requestContextCreator?: (ctx: ParameterizedContext<StateT, CustomT>) => RequestContext;
|
|
72
|
+
middlewares?: Array<IMiddleware<StateT, CustomT>>;
|
|
73
|
+
};
|
|
74
|
+
|
|
57
75
|
/**
|
|
58
76
|
* Bind provided handlers for the OpenAPI routes
|
|
59
77
|
*
|
|
60
78
|
* Koa's default StateT and CustomT values are selected as defaults
|
|
61
|
-
* @param
|
|
62
|
-
* @param spec
|
|
63
|
-
* @param requestContextCreator
|
|
79
|
+
* @param opts Named arguments to the function
|
|
80
|
+
* @param spec Deprecated use the named arguments
|
|
81
|
+
* @param requestContextCreator Deprecated
|
|
64
82
|
*/
|
|
65
83
|
export function bind<Spec, RequestContext = void, StateT = any, CustomT = Record<string, unknown>>(
|
|
66
|
-
|
|
67
|
-
spec
|
|
84
|
+
opts: runtime.server.HandlerFactory<Spec> | Opts<Spec, StateT, CustomT, RequestContext>,
|
|
85
|
+
spec?: Spec,
|
|
68
86
|
requestContextCreator?: (ctx: ParameterizedContext<StateT, CustomT>) => RequestContext
|
|
69
87
|
): Router<StateT, CustomT> {
|
|
88
|
+
const arg =
|
|
89
|
+
typeof opts === 'function'
|
|
90
|
+
? { handler: opts, spec, requestContextCreator, middlewares: [] }
|
|
91
|
+
: opts;
|
|
92
|
+
assert(arg.spec, 'Missing spec argument -- please use the named argument version of bind');
|
|
70
93
|
const router = new Router<StateT, CustomT>();
|
|
71
|
-
handler(adapter(router, requestContextCreator || (() => ({}))))(
|
|
94
|
+
arg.handler(adapter(router, arg.requestContextCreator || (() => ({})), arg.middlewares || []))(
|
|
95
|
+
arg.spec
|
|
96
|
+
);
|
|
72
97
|
return router;
|
|
73
98
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartlyio/oats-koa-adapter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.2+8b5763c",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Koa adapter for Oats",
|
|
6
6
|
"private": false,
|
|
@@ -37,16 +37,16 @@
|
|
|
37
37
|
"client"
|
|
38
38
|
],
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@smartlyio/oats-runtime": "^
|
|
40
|
+
"@smartlyio/oats-runtime": "^4.0.0-alpha.2+8b5763c",
|
|
41
41
|
"@types/koa": "2.13.4",
|
|
42
42
|
"@types/koa-router": "7.4.4",
|
|
43
|
-
"@types/lodash": "4.14.
|
|
44
|
-
"@types/node": "
|
|
43
|
+
"@types/lodash": "4.14.177",
|
|
44
|
+
"@types/node": "16.11.7",
|
|
45
45
|
"koa": "2.13.4",
|
|
46
46
|
"koa-body": "4.2.0",
|
|
47
47
|
"koa-router": "10.1.1",
|
|
48
48
|
"ts-node": "10.4.0",
|
|
49
49
|
"typescript": "4.4.4"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "8b5763c26ee54374d87dbffc37f87dc388370c31"
|
|
52
52
|
}
|