Comparing version 0.1.3 to 0.1.4
@@ -14,8 +14,10 @@ import * as SchemaValidator from "./SchemaValidator"; | ||
}> extends true ? undefined : T; | ||
export default class Endpoint<InputType, OutputType> { | ||
type EndpointCallBack<InputType, OutputType, ErrorType> = (input: InputType, error: (error: ErrorType) => void) => OutputType; | ||
export default class Endpoint<InputType, OutputType, ErrorType> { | ||
method: HTTPMethod; | ||
path: string; | ||
inputType?: Input<SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>; | ||
outputType?: Output<SchemaValidator.AnyTarinType, SchemaValidator.AnyTarinObject, SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>; | ||
callback?: (input: InputType) => OutputType; | ||
outputType?: Output<SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>; | ||
errorType?: SchemaValidator.AnyTarinObject; | ||
callback?: EndpointCallBack<InputType, OutputType, ErrorType>; | ||
constructor(path: string, method: HTTPMethod); | ||
@@ -28,9 +30,10 @@ input<BodyType extends SchemaValidator.AnyTarinObject, QueryType extends SchemaValidator.AnyTarinObject, ParamsType extends SchemaValidator.AnyTarinObject, HeadersType extends SchemaValidator.AnyTarinObject, FilesType extends SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>(inputType: Input<BodyType, QueryType, ParamsType, HeadersType, FilesType>): Endpoint<Simplify<OnlyRequired<{ | ||
files: MakeItBetter<Simplify<GetType<FilesType>>>; | ||
}>>, OutputType>; | ||
output<BodyType extends SchemaValidator.AnyTarinType, HeadersType extends SchemaValidator.AnyTarinObject, FilesType extends SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>(outputType: Output<BodyType, HeadersType, FilesType>): Endpoint<InputType, Simplify<OnlyRequired<{ | ||
}>>, OutputType, ErrorType>; | ||
output<BodyType extends SchemaValidator.AnyTarinObject, HeadersType extends SchemaValidator.AnyTarinObject, FilesType extends SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>(outputType: Output<BodyType, HeadersType, FilesType>): Endpoint<InputType, Simplify<OnlyRequired<{ | ||
body: MakeItBetter<Simplify<GetType<BodyType>>>; | ||
headers: MakeItBetter<Simplify<GetType<HeadersType>>>; | ||
files: MakeItBetter<Simplify<GetType<FilesType>>>; | ||
}>>>; | ||
handleLogic(callback: (input: InputType) => OutputType): Endpoint<InputType, OutputType>; | ||
}>>, ErrorType>; | ||
error<Type extends SchemaValidator.AnyTarinObject>(errorType: Type): Endpoint<InputType, OutputType, MakeItBetter<Simplify<GetType<Type>>>>; | ||
handleLogic(callback: EndpointCallBack<InputType, OutputType, ErrorType>): Endpoint<InputType, OutputType, ErrorType>; | ||
static createGET(path: string): AnyEndpoint; | ||
@@ -58,3 +61,3 @@ static createPOST(path: string): AnyEndpoint; | ||
export type AnyInputType = Input<SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject>; | ||
export type AnyOutputType = Output<SchemaValidator.AnyTarinType, SchemaValidator.AnyTarinObject, SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>; | ||
export type AnyOutputType = Output<SchemaValidator.AnyTarinObject, SchemaValidator.AnyTarinObject, SchemaValidator.GenericTarinObject<SchemaValidator.TarinFile>>; | ||
export declare const endpoint: { | ||
@@ -70,3 +73,3 @@ get: typeof Endpoint.createGET; | ||
}; | ||
export type AnyEndpoint = Endpoint<any, any>; | ||
export type AnyEndpoint = Endpoint<any, any, any>; | ||
export {}; |
@@ -17,2 +17,6 @@ "use strict"; | ||
} | ||
error(errorType) { | ||
this.errorType = errorType; | ||
return this; | ||
} | ||
handleLogic(callback) { | ||
@@ -19,0 +23,0 @@ this.callback = callback; |
@@ -120,3 +120,5 @@ import { HTTPMethod } from "../Endpoint"; | ||
private default; | ||
private responses; | ||
constructor(_default: ResponseObject); | ||
addResponse(statusCode: string, response: ResponseObject): void; | ||
toJSON(): any; | ||
@@ -123,0 +125,0 @@ } |
@@ -246,8 +246,14 @@ "use strict"; | ||
super(); | ||
this.responses = new Map(); | ||
this.default = _default; | ||
} | ||
addResponse(statusCode, response) { | ||
this.responses.set(statusCode, response); | ||
} | ||
toJSON() { | ||
return { | ||
default: this.default.toJSON() | ||
}; | ||
const jsonObject = {}; | ||
this.responses.forEach((response, responseName) => { | ||
jsonObject[responseName] = response.toJSON(); | ||
}); | ||
return Object.assign({ default: this.default.toJSON() }, jsonObject); | ||
} | ||
@@ -254,0 +260,0 @@ } |
@@ -68,3 +68,3 @@ "use strict"; | ||
if (endpoint.outputType) { | ||
this.addResponses(endpoint.outputType, operation); | ||
this.addResponses(endpoint.outputType, operation, endpoint.errorType); | ||
} | ||
@@ -137,3 +137,3 @@ return operation; | ||
} | ||
addResponses(outputType, operation, files) { | ||
addResponses(outputType, operation, errorType) { | ||
let hasFiles = outputType.files != undefined; | ||
@@ -170,2 +170,10 @@ let hasBody = outputType.body != undefined; | ||
const responses = new index_1.ResponsesObject(response); | ||
if (errorType) { | ||
const schema = errorType.toOpenApiSchema(); | ||
const mediaType = new index_1.MediaTypeObject(schema); | ||
const content = new index_1.ContentObject(); | ||
content.addMediaType("application/json", mediaType); | ||
const response = new index_1.ResponseObject(content); | ||
responses.addResponse("400", response); | ||
} | ||
operation.setResponses(responses); | ||
@@ -172,0 +180,0 @@ } |
@@ -10,3 +10,6 @@ "use strict"; | ||
this.handle = (req, res) => { | ||
var _a; | ||
var _a, _b, _c, _d, _e, _f; | ||
const handleError = (error) => { | ||
throw error; | ||
}; | ||
if (!this.endpoint.callback) { | ||
@@ -19,15 +22,10 @@ res.status(500).json({ | ||
} | ||
if (!this.endpoint.inputType) { | ||
if (!this.endpoint.outputType) { | ||
res.status(500).json({ | ||
status: "error", | ||
message: "Both input type and output type were not specified" | ||
}); | ||
return; | ||
} | ||
const output = this.endpoint.callback({}); | ||
res.status(200).setHeaders(this.handleHeaders(output.headers)).json(output.body); | ||
if (!this.endpoint.inputType && !this.endpoint.outputType) { | ||
res.status(500).json({ | ||
status: "error", | ||
message: "Both input type and output type were not specified" | ||
}); | ||
return; | ||
} | ||
const bodyErrors = (_a = this.endpoint.inputType.body) === null || _a === void 0 ? void 0 : _a.validate(req.body); | ||
const bodyErrors = (_b = (_a = this.endpoint.inputType) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.validate(req.body); | ||
if (bodyErrors) { | ||
@@ -42,3 +40,3 @@ res.status(500).json({ | ||
let queryData = {}; | ||
if (this.endpoint.inputType.query) { | ||
if ((_c = this.endpoint.inputType) === null || _c === void 0 ? void 0 : _c.query) { | ||
const errorsOrData = this.endpoint.inputType.query.parse(req.query); | ||
@@ -58,3 +56,3 @@ if (errorsOrData.isFailure()) { | ||
let paramsData = {}; | ||
if (this.endpoint.inputType.params) { | ||
if ((_d = this.endpoint.inputType) === null || _d === void 0 ? void 0 : _d.params) { | ||
const errorsOrData = this.endpoint.inputType.params.parse(req.params); | ||
@@ -66,3 +64,3 @@ if (errorsOrData.isSuccess()) { | ||
let headersData = {}; | ||
if (this.endpoint.inputType.headers) { | ||
if ((_e = this.endpoint.inputType) === null || _e === void 0 ? void 0 : _e.headers) { | ||
const errorsOrData = this.endpoint.inputType.headers.parse(req.headers); | ||
@@ -73,3 +71,3 @@ if (errorsOrData.isSuccess()) { | ||
} | ||
if (this.endpoint.inputType.files) { | ||
if ((_f = this.endpoint.inputType) === null || _f === void 0 ? void 0 : _f.files) { | ||
const files = req.files; | ||
@@ -91,24 +89,31 @@ if (Array.isArray(files)) { | ||
} | ||
const output = this.endpoint.callback({ | ||
query: queryData, | ||
body: req.body, | ||
params: paramsData, | ||
headers: headersData, | ||
}); | ||
if (this.endpoint.outputType && this.endpoint.outputType.files) { | ||
const formData = new form_data_1.default(); | ||
const filesNames = Object.getOwnPropertyNames(output.files); | ||
for (let fileName of filesNames) { | ||
const file = output.files[fileName]; | ||
formData.append(fileName, file.buffer, { filename: fileName, contentType: "application/octet-stream" }); | ||
try { | ||
const output = this.endpoint.callback({ | ||
query: queryData, | ||
body: req.body, | ||
params: paramsData, | ||
headers: headersData, | ||
}, handleError); | ||
if (this.endpoint.outputType && this.endpoint.outputType.files) { | ||
const formData = new form_data_1.default(); | ||
const filesNames = Object.getOwnPropertyNames(output.files); | ||
for (let fileName of filesNames) { | ||
const file = output.files[fileName]; | ||
formData.append(fileName, file.buffer, { filename: fileName, contentType: "application/octet-stream" }); | ||
} | ||
if (output.body) { | ||
formData.append("data", JSON.stringify(output.body), { contentType: "application/json" }); | ||
} | ||
res.status(200) | ||
.setHeaders(this.handleHeaders(Object.assign(Object.assign({}, output.headers), { "Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}` }))) | ||
.send(formData.getBuffer()); | ||
return; | ||
} | ||
if (output.body) { | ||
formData.append("data", JSON.stringify(output.body), { contentType: "application/json" }); | ||
} | ||
res.status(200) | ||
.setHeaders(this.handleHeaders(Object.assign(Object.assign({}, output.headers), { "Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}` }))) | ||
.send(formData.getBuffer()); | ||
res.status(200).setHeaders(this.handleHeaders(output.headers)).json(output.body); | ||
return; | ||
} | ||
res.status(200).setHeaders(this.handleHeaders(output.headers)).json(output.body); | ||
catch (error) { | ||
res.status(400).json(error); | ||
return; | ||
} | ||
}; | ||
@@ -115,0 +120,0 @@ this.endpoint = endpoint; |
{ | ||
"name": "tarin", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "", | ||
@@ -34,2 +34,2 @@ "main": "lib/index.js", | ||
} | ||
} | ||
} |
55758
1456