Socket
Socket
Sign inDemoInstall

koas-core

Package Overview
Dependencies
7
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.0 to 0.4.0

lib/matcher.d.ts

90

lib/index.d.ts

@@ -1,34 +0,68 @@

import * as Koa from 'koa';
import * as compose from 'koa-compose';
import { Middleware } from 'koa';
import { OpenAPIV3 } from 'openapi-types';
import { Validator as Validator_ } from './createDefaultValidator';
import SchemaValidationError_ from './SchemaValidationError';
declare function koas(spec: OpenAPIV3.Document, middlewares?: koas.Plugin[], { createValidator }?: koas.AdvancedOptions): Promise<koas.Middleware>;
export = koas;
declare namespace koas {
const SchemaValidationError: typeof SchemaValidationError_;
type Validator = Validator_;
interface AdvancedOptions {
createValidator?: (spec?: OpenAPIV3.Document) => Validator;
}
interface Context extends Koa.Context {
import { SchemaValidationError, Validator } from './validation';
export { SchemaValidationError, Validator };
export interface AdvancedOptions {
/**
* A function for creating a custom JSON schema validator.
*/
createValidator?: (spec?: OpenAPIV3.Document) => Validator;
}
declare module 'koa' {
interface DefaultContext {
/**
* The OpenAPI specitic context injected by koas.
*/
openApi?: OpenAPIContext;
}
type Middleware<StateT = any, CustomT = {}> = compose.Middleware<Koa.ParameterizedContext<StateT, CustomT & koas.Context>>;
interface OpenAPIContext {
operationObject?: OpenAPIV3.OperationObject;
openApiObject: OpenAPIV3.Document;
pathItemObject?: OpenAPIV3.PathItemObject;
validate: Validator;
}
}
export interface OpenAPIContext {
/**
* A function that takes Koas options and returns Koa Middleware.
*
*/
type Plugin<StateT = any, CustomT = {}> = (options: koas.PluginOptions<StateT, CustomT>) => koas.Middleware<StateT, CustomT>;
interface PluginOptions<StateT = any, CustomT = {}> {
rawSpec: OpenAPIV3.Document;
runAlways: (middleware: koas.Middleware<StateT, CustomT>) => koas.Middleware<StateT, CustomT>;
spec: OpenAPIV3.Document;
validate: Validator;
}
operationObject?: OpenAPIV3.OperationObject;
/**
*
*/
openApiObject: OpenAPIV3.Document;
/**
*
*/
pathItemObject?: OpenAPIV3.PathItemObject;
/**
*
*/
validate: Validator;
}
/**
* A function that takes Koas options and returns Koa Middleware.
*/
export declare type Plugin = (options: PluginOptions) => Middleware;
export interface PluginOptions {
/**
*
*/
rawSpec: OpenAPIV3.Document;
/**
*
*/
runAlways: (middleware: Middleware) => Middleware;
/**
*
*/
spec: OpenAPIV3.Document;
/**
*
*/
validate: Validator;
}
/**
* Create a Koa middleware from Koas middlewares.
*
* @param spec - The OpenAPI document from which to create an API.
* @param middlewares - The Koas middlewares to use for creating an API.
* @param options - Advanced options
*
* @returns Koa middleware that processes requests according the the OpenAPI document.
*/
export declare function koas(spec: OpenAPIV3.Document, middlewares?: Plugin[], { createValidator }?: AdvancedOptions): Promise<Middleware>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.koas = exports.SchemaValidationError = void 0;
const RefParser = require("@apidevtools/json-schema-ref-parser");
const compose = require("koa-compose");
const lodash_1 = require("lodash");
const createDefaultValidator_1 = require("./createDefaultValidator");
const createMatcher_1 = require("./createMatcher");
const SchemaValidationError_1 = require("./SchemaValidationError");
const methods = new Set(['delete', 'get', 'head', 'options', 'patch', 'post', 'put', 'trace']);
const matcher_1 = require("./matcher");
const validation_1 = require("./validation");
Object.defineProperty(exports, "SchemaValidationError", { enumerable: true, get: function () { return validation_1.SchemaValidationError; } });
const methods = new Set([
'delete',
'get',
'head',
'options',
'patch',
'post',
'put',
'trace',
]);
/**

@@ -16,6 +27,7 @@ * This symbol is used internally to specify a middleware should always be run.

*
* @param middleware The middleware to mark.
* @param middleware - The middleware to mark.
* @returns The marked middleware itself.
*/
function markRunAlways(middleware) {
// @ts-expect-error This is an internal hack.
// eslint-disable-next-line no-param-reassign

@@ -25,9 +37,18 @@ middleware[RUN_ALWAYS] = true;

}
async function koas(spec, middlewares = [], { createValidator = createDefaultValidator_1.default } = {}) {
/**
* Create a Koa middleware from Koas middlewares.
*
* @param spec - The OpenAPI document from which to create an API.
* @param middlewares - The Koas middlewares to use for creating an API.
* @param options - Advanced options
*
* @returns Koa middleware that processes requests according the the OpenAPI document.
*/
async function koas(spec, middlewares = [], { createValidator = validation_1.createDefaultValidator } = {}) {
const dereferencedSpec = (await RefParser.dereference(lodash_1.cloneDeep(spec)));
const matchers = Object.entries(dereferencedSpec.paths).map(([pathTemplate, pathItemObject]) => {
const matcher = createMatcher_1.default(pathTemplate, pathItemObject.parameters);
const matcher = matcher_1.createMatcher(pathTemplate, pathItemObject.parameters);
return [matcher, pathItemObject];
});
const validate = await createValidator(spec);
const validate = createValidator(spec);
const injected = middlewares.map((middleware) => middleware({

@@ -40,2 +61,3 @@ rawSpec: spec,

const composed = compose(injected);
// @ts-expect-error This is an internal hack.
const runAlways = compose(injected.filter((middleware) => middleware[RUN_ALWAYS]));

@@ -46,3 +68,3 @@ return (ctx, next) => {

params = matcher(ctx.path);
return !!params;
return Boolean(params);
});

@@ -68,5 +90,2 @@ ctx.openApi = { openApiObject: spec, validate };

}
(function (koas) {
koas.SchemaValidationError = SchemaValidationError_1.default;
})(koas || (koas = {}));
module.exports = koas;
exports.koas = koas;
{
"name": "koas-core",
"version": "0.3.0",
"version": "0.4.0",
"keywords": [

@@ -29,13 +29,19 @@ "koa",

"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.1",
"@types/koa": "^2.11.3",
"@types/lodash": "^4.14.150",
"@apidevtools/json-schema-ref-parser": "^9.0.6",
"@types/koa": "^2.11.4",
"@types/lodash": "^4.14.161",
"koa-compose": "^4.1.0",
"lodash": "^4.17.15",
"openapi-types": "^1.3.5",
"lodash": "^4.17.20",
"openapi-types": "^7.0.1",
"z-schema": "^4.2.3"
},
"devDependencies": {
"axios-test-instance": "^3.1.1"
},
"peerDependencies": {
"koa": "^2.7.0"
"koa": "^2"
},
"engines": {
"node": ">=12.0.0"
}
}

@@ -20,3 +20,3 @@ # Koas-core

const Koa = require('koa');
const koas = require('koas-core');
const { koas } = require('koas-core');

@@ -62,6 +62,10 @@ const spec = {

```js
module.exports = function myPlugin(options) {
function myPlugin(options) {
return (koasPluginOptions) => async (ctx, next) => {
await next();
};
}
module.exports = {
myPlugin,
};

@@ -71,8 +75,12 @@ ```

TypeScript typings are provided. So it is also possible to write plugins using TypeScript. Pretty
much the only type annotation needed is `koas.Plugin` as the return option for the function.
much the only type annotation needed is `Plugin` as the return option for the function.
```ts
import * as koas from 'koas-core';
import { Plugin } from 'koas-core';
function myPlugin(options: myPlugin.Options): koas.Plugin {
export interface MyPluginOptions {
myOption: unknown;
}
export function myPlugin(options: MyPluginOptions): Plugin {
return (koasPluginOptions) => async (ctx, next) => {

@@ -82,8 +90,2 @@ await next();

}
export = myPlugin;
namespace myPlugin {
export interface Options {}
}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc