Comparing version 0.0.2 to 0.0.3-rc-20230303124120-92013f5
@@ -6,2 +6,2 @@ import { TypedRequest, TypedResponse } from './typed-fetch'; | ||
[TKey: string]: never; | ||
}>({ title, description, version, oasEndpoint, swaggerUIEndpoint, ajv, jsonSerializerFactory, plugins: userPlugins, ...options }?: RouterOptions<TServerContext>): Router<TServerContext, TRouterSDK>; | ||
}>({ title, description, version, oasEndpoint, swaggerUIEndpoint, plugins: userPlugins, ...options }?: RouterOptions<TServerContext>): Router<TServerContext, TRouterSDK>; |
13
index.js
@@ -5,4 +5,9 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
const DefaultFetchAPI = require('@whatwg-node/fetch'); | ||
const server = require('@whatwg-node/server'); | ||
const Ajv = _interopDefault(require('ajv')); | ||
const addFormats = _interopDefault(require('ajv-formats')); | ||
const jsonSerializerFactory = _interopDefault(require('fast-json-stringify')); | ||
@@ -85,3 +90,5 @@ // This allows us to hook into serialization of the response body | ||
function useAjv({ ajv, jsonSerializerFactory }) { | ||
function useAjv() { | ||
const ajv = new Ajv(); | ||
addFormats(ajv); | ||
const serializersByCtx = new WeakMap(); | ||
@@ -499,3 +506,3 @@ return { | ||
} | ||
function createRouter({ title = 'FETS API', description = 'An API written with FETS', version = '1.0.0', oasEndpoint = '/openapi.json', swaggerUIEndpoint = '/docs', ajv, jsonSerializerFactory, plugins: userPlugins = [], ...options } = {}) { | ||
function createRouter({ title = 'FETS API', description = 'An API written with FETS', version = '1.0.0', oasEndpoint = '/openapi.json', swaggerUIEndpoint = '/docs', plugins: userPlugins = [], ...options } = {}) { | ||
const plugins = [ | ||
@@ -519,3 +526,3 @@ ...(oasEndpoint || swaggerUIEndpoint | ||
: []), | ||
...(ajv ? [useAjv({ ajv, jsonSerializerFactory })] : []), | ||
useAjv(), | ||
...userPlugins, | ||
@@ -522,0 +529,0 @@ ]; |
@@ -1,7 +0,2 @@ | ||
import type Ajv from 'ajv'; | ||
import { JSONSchema, JSONSerializer, RouterPlugin } from '../types'; | ||
export interface AJVPluginOptions { | ||
ajv: Ajv; | ||
jsonSerializerFactory?: (schema: JSONSchema) => JSONSerializer; | ||
} | ||
export declare function useAjv({ ajv, jsonSerializerFactory }: AJVPluginOptions): RouterPlugin<any>; | ||
import { RouterPlugin } from '../types'; | ||
export declare function useAjv(): RouterPlugin<any>; |
{ | ||
"name": "fets", | ||
"version": "0.0.2", | ||
"version": "0.0.3-rc-20230303124120-92013f5", | ||
"description": "TypeScript HTTP Framework focusing on e2e type-safety, easy setup, performance & great developer experience", | ||
@@ -9,2 +9,5 @@ "sideEffects": false, | ||
"@whatwg-node/server": "^0.7.2", | ||
"ajv": "^8.12.0", | ||
"ajv-formats": "^2.1.1", | ||
"fast-json-stringify": "^5.6.2", | ||
"json-schema-to-ts": "2.7.2", | ||
@@ -14,6 +17,2 @@ "openapi-types": "12.1.0", | ||
}, | ||
"optionalDependencies": { | ||
"ajv": "^8.12.0", | ||
"ajv-formats": "^2.1.1" | ||
}, | ||
"repository": { | ||
@@ -20,0 +19,0 @@ "type": "git", |
@@ -100,3 +100,3 @@ # FETS | ||
}, | ||
response: { | ||
responses: { | ||
200: { | ||
@@ -517,3 +517,3 @@ type: 'object', | ||
}, | ||
response: { | ||
responses: { | ||
200: { | ||
@@ -564,39 +564,12 @@ type: 'array', | ||
### Runtime validation with Ajv | ||
### Runtime validation | ||
The library itself doesn't include a runtime validation by default. But you can use that plugin to | ||
have a runtime validation with the provided JSON Schemas above. This plugin uses | ||
[Ajv](https://ajv.js.org/) under the hood. All you have to do is to install `ajv` and `ajv-formats` | ||
packages and pass `Ajv` to the router. | ||
FETS uses [`ajv`](https://ajv.js.org/) to validate the request and response bodies at runtime. | ||
```ts | ||
import Ajv from 'ajv' | ||
import { addFormats } from 'ajv-formats' | ||
import { createRouter } from 'fets' | ||
### Safe and faster JSON serialization with response schemas | ||
const ajv = new Ajv() | ||
// Some type issues with Ajv | ||
addFormats(ajv) | ||
FETS uses [`fast-json-stringify`](https://github.com/fastify/fast-json-stringify) that serializes | ||
JavaScript objects into JSON 2x faster than `JSON.stringify` by using JSON Schemas. All you have to | ||
do is to define a correct response schema. | ||
const router = createRouter({ | ||
ajv | ||
}) | ||
``` | ||
### Safe and faster JSON serialization with `fast-json-stringify` | ||
[`fast-json-stringify`](https://github.com/fastify/fast-json-stringify) is a library that serializes | ||
JavaScript objects into JSON 2x faster than `JSON.stringify` by using JSON Schemas. So FETS can use | ||
that library to serialize the response body. All you have to do is to install `fast-json-stringify` | ||
package and pass `fastJsonStringify` to the router. | ||
```ts | ||
import jsonSerializerFactory from 'fast-json-stringify' | ||
import { createRouter } from 'fets' | ||
const router = createRouter({ | ||
jsonSerializerFactory | ||
}) | ||
``` | ||
### OpenAPI Generation | ||
@@ -637,3 +610,3 @@ | ||
schemas: { | ||
response: { | ||
responses: { | ||
200: Type.Array(Todo) | ||
@@ -640,0 +613,0 @@ } |
@@ -1,2 +0,1 @@ | ||
import type Ajv from 'ajv'; | ||
import { FromSchema as FromSchemaOriginal, JSONSchema as JSONSchemaOrBoolean } from 'json-schema-to-ts'; | ||
@@ -17,4 +16,2 @@ import { ServerAdapter, ServerAdapterOptions, ServerAdapterPlugin, ServerAdapterRequestHandler } from '@whatwg-node/server'; | ||
swaggerUIEndpoint?: string | false; | ||
ajv?: Ajv; | ||
jsonSerializerFactory?: (jsonSchema: any) => JSONSerializer; | ||
} | ||
@@ -21,0 +18,0 @@ export type FromSchema<T> = T extends JSONSchema ? FromSchemaOriginal<T, { |
Sorry, the diff of this file is not supported yet
1551
98818
8
678
+ Addedajv@^8.12.0
+ Addedajv-formats@^2.1.1
+ Addedfast-json-stringify@^5.6.2
+ Added@fastify/merge-json-schemas@0.1.1(transitive)
+ Addedajv-formats@3.0.1(transitive)
+ Addedfast-json-stringify@5.16.1(transitive)
+ Addedfast-uri@2.4.0(transitive)
+ Addedjson-schema-ref-resolver@1.0.1(transitive)
+ Addedrfdc@1.4.1(transitive)