Comparing version 2.0.41 to 2.0.42
@@ -9,4 +9,4 @@ type AnyObject = Record<any, any>; | ||
headers: AnyObject; | ||
file: AnyObject; | ||
mid: AnyObject; | ||
files: AnyObject; | ||
middlewares: AnyObject; | ||
} | ||
@@ -13,0 +13,0 @@ export interface Handler { |
import { AbstractHandler, Handler } from '../handler'; | ||
import { DataParser } from './data-validator'; | ||
import { FileConfig } from './file-validator'; | ||
import { FilesConfig } from './file-validator'; | ||
export interface BridgeHandlerDocumentation { | ||
@@ -14,5 +14,4 @@ title?: string; | ||
headersSchema?: DataParser; | ||
fileConfig?: FileConfig; | ||
filesConfig?: FilesConfig; | ||
middlewares?: Middlewares; | ||
documentation?: BridgeHandlerDocumentation; | ||
}; | ||
@@ -26,5 +25,4 @@ private handler; | ||
headersSchema?: DataParser; | ||
fileConfig?: FileConfig; | ||
filesConfig?: FilesConfig; | ||
middlewares?: Middlewares; | ||
documentation?: BridgeHandlerDocumentation; | ||
}); | ||
@@ -31,0 +29,0 @@ /** |
@@ -17,11 +17,3 @@ "use strict"; | ||
this.resolve = config.resolve; | ||
if (config.bodySchema && !config.resolve.length) | ||
throw Error(`You can't have no argument in your resolve function with a bodySchema specification`); | ||
if (config.querySchema && !config.resolve.length) | ||
throw Error(`You can't have no argument in your resolve function with a querySchema specification`); | ||
if (config.headersSchema && !config.resolve.length) | ||
throw Error(`You can't have no argument in your resolve function with a headersSchema specification`); | ||
if (config.fileConfig && !config.resolve.length) | ||
throw Error(`You can't have no argument in your resolve function with a fileConfig specification`); | ||
if (config.bodySchema && config.fileConfig) | ||
if (config.bodySchema && config.filesConfig) | ||
throw Error("You can't get a JSON body and files in the same endpoint."); | ||
@@ -36,4 +28,4 @@ const firstHandler = new handler_1.FirstHandler(); | ||
handler = handler.setNext(new data_validator_1.DataValidator(config.headersSchema, 'headers')); | ||
if (config.fileConfig) | ||
handler = handler.setNext(new file_validator_1.FileValidator(config.fileConfig)); | ||
if (config.filesConfig) | ||
handler = handler.setNext(new file_validator_1.FileValidator(config.filesConfig)); | ||
if (config.middlewares) | ||
@@ -54,3 +46,3 @@ handler = handler.setNext(new middleware_1.MiddelwaresHandler(config.middlewares)); | ||
return res; | ||
data.mid = { ...res, ...data.mid }; | ||
data.middlewares = { ...res, ...data.middlewares }; | ||
if (this.nextHandler) | ||
@@ -57,0 +49,0 @@ return this.nextHandler.handle(data); |
import { AbstractHandler, Handler } from '../handler'; | ||
export type FileConfig = 'any' | ReadonlyArray<string>; | ||
export type FilesConfig = 'any' | ReadonlyArray<string>; | ||
export declare class FileValidator extends AbstractHandler { | ||
private config; | ||
constructor(config: FileConfig); | ||
constructor(config: FilesConfig); | ||
handle: Handler['handle']; | ||
} | ||
//# sourceMappingURL=file-validator.d.ts.map |
@@ -17,3 +17,3 @@ "use strict"; | ||
for (const name of this.config) | ||
if (!data.file[name]) | ||
if (!data.files[name]) | ||
missingFiles.push(name); | ||
@@ -20,0 +20,0 @@ if (missingFiles.length > 0) |
import { Handler, AbstractHandler } from '../handler'; | ||
/** | ||
* TO COMPLETE | ||
*/ | ||
export declare class MiddelwaresHandler extends AbstractHandler { | ||
@@ -6,0 +3,0 @@ private handlers; |
@@ -6,5 +6,2 @@ "use strict"; | ||
const error_1 = require("../../error"); | ||
/** | ||
* TO COMPLETE | ||
*/ | ||
class MiddelwaresHandler extends handler_1.AbstractHandler { | ||
@@ -23,3 +20,3 @@ handlers; | ||
return result; | ||
return super.handle({ ...data, mid: Object.assign({}, ...results) }); | ||
return super.handle({ ...data, middlewares: Object.assign({}, ...results) }); | ||
}; | ||
@@ -26,0 +23,0 @@ } |
@@ -25,5 +25,5 @@ "use strict"; | ||
headersSchema: routeParams.headers, | ||
fileConfig: routeParams.file, | ||
filesConfig: routeParams.files, | ||
middlewares: routeParams.middlewares, | ||
documentation: routeParams.documentation, | ||
// documentation: routeParams.documentation, | ||
resolve: routeParams.resolve, | ||
@@ -30,0 +30,0 @@ }); |
import { KeysWithValNotEmptyObject, MidsReturnsIntersection, MidsParams } from '../utilities'; | ||
import { DataParser, BridgeHandler, InferDataParser, BridgeHandlerDocumentation, FileConfig } from './handlers'; | ||
import { DataParser, BridgeHandler, InferDataParser, FilesConfig } from './handlers'; | ||
import { FormidableFile } from '../utilities'; | ||
export interface BridgeParams<Resolve = any, Mids extends ReadonlyArray<BridgeHandler> = never, Body extends DataParser<Record<any, any>> = never, Query extends DataParser<Record<any, any>> = never, Headers extends DataParser<Record<any, any>> = never, File extends FileConfig = ['BridgeFilesDoNotExists']> { | ||
export interface BridgeParams<Resolve = any, Middlewares extends ReadonlyArray<BridgeHandler<(p: any) => Record<any, any>>> = never, Body extends DataParser<Record<any, any>> = never, Query extends DataParser<Record<any, any>> = never, Headers extends DataParser<Record<any, any>> = never, Files extends FilesConfig = ['BridgeFilesDoNotExists']> { | ||
resolve: Resolve; | ||
middlewares?: Mids; | ||
middlewares?: Middlewares; | ||
body?: Body /** Can't have a body with GET method or with files, an error is throw if ther developer tries to, but the type here doesnt block to keep a clean UI */; | ||
query?: Query; | ||
headers?: Headers; | ||
file?: File; | ||
documentation?: BridgeHandlerDocumentation; | ||
files?: Files; | ||
} | ||
export type CreateHandler = <Resolve extends (p: { | ||
[key in KeysWithValNotEmptyObject<{ | ||
mid: MidsReturnsIntersection<Mids> extends never ? {} : MidsReturnsIntersection<Mids>; | ||
body: (InferDataParser<Body> extends never ? {} : InferDataParser<Body>) & (MidsParams<Mids>['body'] extends never ? {} : MidsParams<Mids>['body']); | ||
query: (InferDataParser<Query> extends never ? {} : InferDataParser<Query>) & (MidsParams<Mids>['query'] extends never ? {} : MidsParams<Mids>['query']); | ||
headers: (InferDataParser<Headers> extends never ? {} : InferDataParser<Headers>) & (MidsParams<Mids>['headers'] extends never ? {} : MidsParams<Mids>['headers']); | ||
file: File extends ['BridgeFilesDoNotExists'] ? {} : File extends 'any' ? { | ||
middlewares: MidsReturnsIntersection<Middelwares> extends never ? {} : MidsReturnsIntersection<Middelwares>; | ||
body: (InferDataParser<Body> extends never ? {} : InferDataParser<Body>) & (MidsParams<Middelwares>['body'] extends never ? {} : MidsParams<Middelwares>['body']); | ||
query: (InferDataParser<Query> extends never ? {} : InferDataParser<Query>) & (MidsParams<Middelwares>['query'] extends never ? {} : MidsParams<Middelwares>['query']); | ||
headers: (InferDataParser<Headers> extends never ? {} : InferDataParser<Headers>) & (MidsParams<Middelwares>['headers'] extends never ? {} : MidsParams<Middelwares>['headers']); | ||
files: Files extends ['BridgeFilesDoNotExists'] ? {} : Files extends 'any' ? { | ||
[key: string]: FormidableFile; | ||
} : { | ||
[key in File[number]]: FormidableFile; | ||
[key in Files[number]]: FormidableFile; | ||
}; | ||
}> & keyof { | ||
mid: MidsReturnsIntersection<Mids> extends never ? {} : MidsReturnsIntersection<Mids>; | ||
body: (InferDataParser<Body> extends never ? {} : InferDataParser<Body>) & (MidsParams<Mids>['body'] extends never ? {} : MidsParams<Mids>['body']); | ||
query: (InferDataParser<Query> extends never ? {} : InferDataParser<Query>) & (MidsParams<Mids>['query'] extends never ? {} : MidsParams<Mids>['query']); | ||
headers: (InferDataParser<Headers> extends never ? {} : InferDataParser<Headers>) & (MidsParams<Mids>['headers'] extends never ? {} : MidsParams<Mids>['headers']); | ||
file: File extends ['BridgeFilesDoNotExists'] ? {} : File extends 'any' ? { | ||
middlewares: MidsReturnsIntersection<Middelwares> extends never ? {} : MidsReturnsIntersection<Middelwares>; | ||
body: (InferDataParser<Body> extends never ? {} : InferDataParser<Body>) & (MidsParams<Middelwares>['body'] extends never ? {} : MidsParams<Middelwares>['body']); | ||
query: (InferDataParser<Query> extends never ? {} : InferDataParser<Query>) & (MidsParams<Middelwares>['query'] extends never ? {} : MidsParams<Middelwares>['query']); | ||
headers: (InferDataParser<Headers> extends never ? {} : InferDataParser<Headers>) & (MidsParams<Middelwares>['headers'] extends never ? {} : MidsParams<Middelwares>['headers']); | ||
files: Files extends ['BridgeFilesDoNotExists'] ? {} : Files extends 'any' ? { | ||
[key: string]: FormidableFile; | ||
} : { | ||
[key in File[number]]: FormidableFile; | ||
[key in Files[number]]: FormidableFile; | ||
}; | ||
}]: { | ||
mid: MidsReturnsIntersection<Mids> extends never ? {} : MidsReturnsIntersection<Mids>; | ||
body: (InferDataParser<Body> extends never ? {} : InferDataParser<Body>) & (MidsParams<Mids>['body'] extends never ? {} : MidsParams<Mids>['body']); | ||
query: (InferDataParser<Query> extends never ? {} : InferDataParser<Query>) & (MidsParams<Mids>['query'] extends never ? {} : MidsParams<Mids>['query']); | ||
headers: (InferDataParser<Headers> extends never ? {} : InferDataParser<Headers>) & (MidsParams<Mids>['headers'] extends never ? {} : MidsParams<Mids>['headers']); | ||
file: File extends ['BridgeFilesDoNotExists'] ? {} : File extends 'any' ? { | ||
middlewares: MidsReturnsIntersection<Middelwares> extends never ? {} : MidsReturnsIntersection<Middelwares>; | ||
body: (InferDataParser<Body> extends never ? {} : InferDataParser<Body>) & (MidsParams<Middelwares>['body'] extends never ? {} : MidsParams<Middelwares>['body']); | ||
query: (InferDataParser<Query> extends never ? {} : InferDataParser<Query>) & (MidsParams<Middelwares>['query'] extends never ? {} : MidsParams<Middelwares>['query']); | ||
headers: (InferDataParser<Headers> extends never ? {} : InferDataParser<Headers>) & (MidsParams<Middelwares>['headers'] extends never ? {} : MidsParams<Middelwares>['headers']); | ||
files: Files extends ['BridgeFilesDoNotExists'] ? {} : Files extends 'any' ? { | ||
[key: string]: FormidableFile; | ||
} : { | ||
[key in File[number]]: FormidableFile; | ||
[key in Files[number]]: FormidableFile; | ||
}; | ||
}[key]; | ||
}) => Res, Res, Body extends DataParser<Record<any, any>> = never, Query extends DataParser<Record<string, any>> = never, Headers extends DataParser<Record<string, any>> = never, File extends FileConfig = ['BridgeFilesDoNotExists'], Mids extends ReadonlyArray<BridgeHandler> = never>(p: BridgeParams<Resolve, Mids, Body, Query, Headers, File>) => BridgeHandler<Resolve, Mids>; | ||
}) => Res, Res, Body extends DataParser<Record<any, any>> = never, Query extends DataParser<Record<string, any>> = never, Headers extends DataParser<Record<string, any>> = never, Files extends FilesConfig = ['BridgeFilesDoNotExists'], Middelwares extends ReadonlyArray<BridgeHandler> = never>(p: BridgeParams<Resolve, Middelwares, Body, Query, Headers, Files>) => BridgeHandler<Resolve, Middelwares>; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -7,2 +7,5 @@ interface Error { | ||
export declare const isBridgeError: (object: any) => object is Error; | ||
export declare const isError: (object: any) => object is { | ||
error: Error; | ||
}; | ||
/** | ||
@@ -9,0 +12,0 @@ * |
@@ -17,3 +17,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.httpError = exports.isBridgeError = void 0; | ||
exports.httpError = exports.isError = exports.isBridgeError = void 0; | ||
const isBridgeError = (object) => typeof object === 'object' && | ||
@@ -24,2 +24,4 @@ typeof object.error === 'object' && | ||
exports.isBridgeError = isBridgeError; | ||
const isError = (object) => typeof object === 'object' && (0, exports.isBridgeError)(object.error); | ||
exports.isError = isError; | ||
/** | ||
@@ -26,0 +28,0 @@ * |
export { handler } from './core'; | ||
export { httpError, onError, StatusCode, isBridgeError } from './error'; | ||
export { httpError, onError, StatusCode, isError } from './error'; | ||
export { apply } from './utilities'; | ||
@@ -4,0 +4,0 @@ export { initBridge } from './bridge'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.method = exports.initBridge = exports.apply = exports.isBridgeError = exports.StatusCode = exports.onError = exports.httpError = exports.handler = void 0; | ||
exports.method = exports.initBridge = exports.apply = exports.isError = exports.StatusCode = exports.onError = exports.httpError = exports.handler = void 0; | ||
var core_1 = require("./core"); | ||
@@ -10,3 +10,3 @@ Object.defineProperty(exports, "handler", { enumerable: true, get: function () { return core_1.handler; } }); | ||
Object.defineProperty(exports, "StatusCode", { enumerable: true, get: function () { return error_1.StatusCode; } }); | ||
Object.defineProperty(exports, "isBridgeError", { enumerable: true, get: function () { return error_1.isBridgeError; } }); | ||
Object.defineProperty(exports, "isError", { enumerable: true, get: function () { return error_1.isError; } }); | ||
var utilities_1 = require("./utilities"); | ||
@@ -13,0 +13,0 @@ Object.defineProperty(exports, "apply", { enumerable: true, get: function () { return utilities_1.apply; } }); |
import { BridgeRoutes, ServerRoutes } from './types'; | ||
export declare const convertBridgeRoutesToServerRoutes: (routes: BridgeRoutes, prefix?: string) => ServerRoutes; | ||
export declare const convertBridgeRoutesToServerRoutes: (routes: BridgeRoutes, prefix: string, hasFormidable: boolean) => ServerRoutes; | ||
//# sourceMappingURL=convert.d.ts.map |
@@ -8,12 +8,21 @@ "use strict"; | ||
const serverRoutes = {}; | ||
const convertBridgeRoutesToServerRoutes = (routes, prefix = '') => { | ||
const convertBridgeRoutesToServerRoutes = (routes, prefix, hasFormidable) => { | ||
for (const [key, value] of Object.entries(routes)) { | ||
if (!value) | ||
continue; | ||
else if ((0, method_1.isBridgeMethod)(value)) | ||
else if ((0, method_1.isBridgeMethod)(value)) { | ||
serverRoutes[`${prefix}/${key}`] = value.methods; | ||
else if ((0, core_1.isBridgeHandler)(value)) | ||
Object.values(value.methods).forEach((handler) => { | ||
if (handler.config.filesConfig && !hasFormidable) | ||
throw new Error(`You need to install formidable and to give it to Bridge in order to use files.`); | ||
}); | ||
} | ||
else if ((0, core_1.isBridgeHandler)(value)) { | ||
serverRoutes[`${prefix}/${key}`] = { POST: value }; | ||
if (value.config.filesConfig && !hasFormidable) | ||
throw new Error(`You need to install formidable and pass it to the initBridge function in order to use files. | ||
\nCheck the documentation: https://bridge.codes/docs/file \n`); | ||
} | ||
else if ((0, types_1.isBridgeRoutes)(value)) | ||
(0, exports.convertBridgeRoutesToServerRoutes)(value, `${prefix}/${key}`); | ||
(0, exports.convertBridgeRoutesToServerRoutes)(value, `${prefix}/${key}`, hasFormidable); | ||
} | ||
@@ -20,0 +29,0 @@ return serverRoutes; |
@@ -21,3 +21,3 @@ import { BridgeHandler } from '../core'; | ||
headers: Parameters<ResolveFct>[0]['headers']; | ||
file: Parameters<ResolveFct>[0]['file']; | ||
files: Parameters<ResolveFct>[0]['files']; | ||
return: (ReturnType<ResolveFct> extends Promise<infer RetWithoutPromise> ? RetWithoutPromise : ReturnType<ResolveFct>) | Extract<BridgeHandlerReturnType<Middlewares[number]>['return'], { | ||
@@ -28,3 +28,3 @@ error: any; | ||
name: 'Body schema validation error'; | ||
status: 422; | ||
status: 400; | ||
data: any; | ||
@@ -35,3 +35,3 @@ }; | ||
name: 'Headers schema validation error'; | ||
status: 422; | ||
status: 400; | ||
data: any; | ||
@@ -42,3 +42,3 @@ }; | ||
name: 'Query schema validation error'; | ||
status: 422; | ||
status: 400; | ||
data: any; | ||
@@ -45,0 +45,0 @@ }; |
@@ -9,6 +9,6 @@ "use strict"; | ||
let queryString; | ||
const serverRoutes = (0, routes_1.convertBridgeRoutesToServerRoutes)(routes); | ||
const serverRoutes = (0, routes_1.convertBridgeRoutesToServerRoutes)(routes, '', config?.formidable !== undefined); | ||
return async (req, res) => { | ||
let body = {}; | ||
let file = {}; | ||
let files = {}; | ||
const query = (0, http_transormers_1.getJSONQueryFromURL)(req.url || ''); | ||
@@ -23,15 +23,12 @@ try { | ||
.end(JSON.stringify({ status: 404, name: 'Route not found' })); | ||
if (endpoint.config.fileConfig && !config?.formidable) | ||
throw new Error(`You need to install formidable and to give it to Bridge in order to use files.`); | ||
if (endpoint.config.fileConfig) | ||
file = await (0, http_transormers_1.formidableAsyncParseFiles)(req, config?.formidable); | ||
if (endpoint.config.filesConfig) | ||
files = await (0, http_transormers_1.formidableAsyncParseFiles)(req, config?.formidable); | ||
else | ||
body = await (0, http_transormers_1.getJSONDataFromRequestStream)(req); | ||
const mid = {}; | ||
const result = await endpoint.handle({ | ||
body, | ||
file, | ||
files, | ||
query, | ||
headers: req.headers, | ||
mid, | ||
middlewares: {}, | ||
}); | ||
@@ -38,0 +35,0 @@ if (!result) |
@@ -5,2 +5,4 @@ "use strict"; | ||
const getJSONDataFromRequestStream = (request) => new Promise((resolve) => { | ||
if (typeof request.body === 'object') | ||
resolve(request.body); | ||
const chunks = []; | ||
@@ -7,0 +9,0 @@ request.on('data', (chunk) => { |
import { BridgeHandler } from '../core'; | ||
import { UnionToArray } from './types'; | ||
type Apply = <B extends string | BridgeHandler, T extends Array<B>>(...args: T) => B extends string ? UnionToArray<T[number]> : UnionToArray<T[number]>; | ||
type Apply = <B extends string | BridgeHandler<(p: any) => Record<any, any>>, T extends Array<B>>(...args: T) => B extends string ? UnionToArray<T[number]> : UnionToArray<T[number]>; | ||
export declare const apply: Apply; | ||
export {}; | ||
//# sourceMappingURL=apply.d.ts.map |
{ | ||
"name": "bridge", | ||
"version": "2.0.41", | ||
"version": "2.0.42", | ||
"description": "Bridge is a Typescript Node.js framework that provides an easy and scalable way to create REST APIs while generating the client code.", | ||
@@ -36,10 +36,8 @@ "author": "Bridge Team <support@bridge.codes>", | ||
"devDependencies": { | ||
"@types/express": "^4.17.14", | ||
"@types/express": "^4.17.17", | ||
"@types/node": "^18.11.15", | ||
"express": "^4.18.2", | ||
"nodemon": "^2.0.20", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.3" | ||
}, | ||
"dependencies": {} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120863
5
130
1230