@smartlyio/oats-koa-adapter 4.0.2 → 4.0.3

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.
@@ -0,0 +1,20 @@
1
+ import * as Router from 'koa-router';
2
+ import { ParameterizedContext } from 'koa';
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
+ };
11
+ /**
12
+ * Bind provided handlers for the OpenAPI routes
13
+ *
14
+ * Koa's default StateT and CustomT values are selected as defaults
15
+ * @param opts Named arguments to the function
16
+ * @param spec Deprecated use the named arguments
17
+ * @param requestContextCreator Deprecated
18
+ */
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 ADDED
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bind = void 0;
4
+ const Router = require("koa-router");
5
+ const runtime = require("@smartlyio/oats-runtime");
6
+ const assert = require("assert");
7
+ function adapter(router, requestContextCreator, middlewares) {
8
+ return (path, op, method, handler) => {
9
+ const koaPath = path.replace(/{([^}]+)}/g, (m, param) => ':' + param);
10
+ router[method](koaPath, ...middlewares, async (ctx) => {
11
+ const files = ctx.request.files;
12
+ let fileFields = {};
13
+ if (files) {
14
+ fileFields = Object.keys(files).reduce((memo, name) => {
15
+ memo[name] = new runtime.make.File(files[name].path, files[name].size, files[name].name);
16
+ return memo;
17
+ }, {});
18
+ }
19
+ const contentType = ctx.request.type;
20
+ const requestBody = ctx.request.body;
21
+ let body;
22
+ if (Array.isArray(requestBody)) {
23
+ body = { value: requestBody, contentType };
24
+ }
25
+ else if (typeof requestBody === 'object') {
26
+ const bodyWithFileFields = Object.assign(Object.assign({}, requestBody), fileFields);
27
+ body =
28
+ Object.keys(bodyWithFileFields).length > 0
29
+ ? { value: bodyWithFileFields, contentType }
30
+ : undefined;
31
+ }
32
+ const result = await handler({
33
+ path,
34
+ method: runtime.server.assertMethod(ctx.method.toLowerCase()),
35
+ servers: [],
36
+ op,
37
+ headers: ctx.request.headers,
38
+ params: ctx.params,
39
+ query: ctx.query,
40
+ body,
41
+ requestContext: requestContextCreator(ctx)
42
+ });
43
+ ctx.body = result.value.value;
44
+ ctx.status = result.status;
45
+ ctx.set(result.headers);
46
+ });
47
+ };
48
+ }
49
+ /**
50
+ * Bind provided handlers for the OpenAPI routes
51
+ *
52
+ * Koa's default StateT and CustomT values are selected as defaults
53
+ * @param opts Named arguments to the function
54
+ * @param spec Deprecated use the named arguments
55
+ * @param requestContextCreator Deprecated
56
+ */
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');
62
+ const router = new Router();
63
+ arg.handler(adapter(router, arg.requestContextCreator || (() => ({})), arg.middlewares || []))(arg.spec);
64
+ return router;
65
+ }
66
+ exports.bind = bind;
67
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
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,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AC5F,GAAG,CAAC,IAAI,CACT,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AAfD,oBAeC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ /* eslint-disable no-console */
3
+ /* eslint-disable @typescript-eslint/no-floating-promises */
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const fs = require("fs");
6
+ const child = require("child_process");
7
+ const assert = require("assert");
8
+ const util = require("util");
9
+ const md = fs.readFileSync('README.template.md', 'utf8');
10
+ const examples = [];
11
+ fs.writeFileSync('README.md', md.replace(/^>>(.*)/gm, (match, file) => {
12
+ const m = file.trim();
13
+ const example = fs.readFileSync('./' + m, 'utf8');
14
+ const runner = example.split('\n')[0].match(/\/\/(.*)/)[1];
15
+ assert(runner, 'missing runner command');
16
+ examples.push(runner.trim());
17
+ return example;
18
+ }));
19
+ Promise.all(examples.map(async (example) => {
20
+ console.log('testing ' + example);
21
+ try {
22
+ await util.promisify(child.exec)(example);
23
+ return example + ': ok';
24
+ }
25
+ catch (e) {
26
+ return example + ': error ' + e.message;
27
+ }
28
+ })).then(results => {
29
+ const errors = results.filter(result => /: error/.test(result));
30
+ if (errors.length > 0) {
31
+ console.error('errors in rendering');
32
+ errors.map(error => console.error(error));
33
+ process.exit(1);
34
+ }
35
+ results.map(result => console.log(result));
36
+ });
37
+ //# sourceMappingURL=render-docs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render-docs.js","sourceRoot":"","sources":["../render-docs.ts"],"names":[],"mappings":";AAAA,+BAA+B;AAC/B,4DAA4D;;AAE5D,yBAAyB;AACzB,uCAAuC;AACvC,iCAAiC;AACjC,6BAA6B;AAE7B,MAAM,EAAE,GAAW,EAAE,CAAC,YAAY,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAC;AACjE,MAAM,QAAQ,GAAa,EAAE,CAAC;AAC9B,EAAE,CAAC,aAAa,CACd,WAAW,EACX,EAAE,CAAC,OAAO,CAAC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,CAAC,CAAC,CAAC;QAC1C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACjB;IACD,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7C,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartlyio/oats-koa-adapter",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "license": "MIT",
5
5
  "description": "Koa adapter for Oats",
6
6
  "private": false,
@@ -37,7 +37,7 @@
37
37
  "client"
38
38
  ],
39
39
  "devDependencies": {
40
- "@smartlyio/oats-runtime": "^4.0.2",
40
+ "@smartlyio/oats-runtime": "^4.0.3",
41
41
  "@types/koa": "2.13.4",
42
42
  "@types/koa-router": "7.4.4",
43
43
  "@types/lodash": "4.14.178",
@@ -48,5 +48,5 @@
48
48
  "ts-node": "10.4.0",
49
49
  "typescript": "4.5.4"
50
50
  },
51
- "gitHead": "140ea78c4bb8bd34c7959941d4f9d771c289c30c"
51
+ "gitHead": "5897af3aa6bea0c602978a10816c577a6b051c90"
52
52
  }