diff --git a/src/libs/DefaultSchemas.js b/src/libs/DefaultSchemas.ts similarity index 83% rename from src/libs/DefaultSchemas.js rename to src/libs/DefaultSchemas.ts index 6cb05c9..4ac495b 100644 --- a/src/libs/DefaultSchemas.js +++ b/src/libs/DefaultSchemas.ts @@ -1,6 +1,6 @@ -'use strict'; +import type { TFMASchemas, ajvSchema } from "../types/index.js"; -const defaultSchemas = modelName => { +const defaultSchemas = (modelName: string): TFMASchemas => { return { routeGet: { summary: 'Get details of single ' + modelName, @@ -12,7 +12,8 @@ const defaultSchemas = modelName => { type: 'string', description: 'Unique identifier of ' + modelName } - } + }, + }, querystring: { type: 'object', @@ -24,8 +25,8 @@ const defaultSchemas = modelName => { } }, response: { - 404: { $ref: 'MongooseApiDefErrRespSchemas404#' }, - 500: { $ref: 'MongooseApiDefErrRespSchemas500#' } + 404: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas404#' }, + 500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' } } }, routePost: { @@ -41,7 +42,7 @@ const defaultSchemas = modelName => { } }, response: { - 500: { $ref: 'MongooseApiDefErrRespSchemas500#' } + 500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' } } }, routeList: { @@ -90,7 +91,7 @@ const defaultSchemas = modelName => { } }, response: { - 500: { $ref: 'MongooseApiDefErrRespSchemas500#' } + 500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' } } }, routePut: { @@ -115,8 +116,8 @@ const defaultSchemas = modelName => { } }, response: { - 404: { $ref: 'MongooseApiDefErrRespSchemas404#' }, - 500: { $ref: 'MongooseApiDefErrRespSchemas500#' } + 404: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas404#' }, + 500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' } } }, routePatch: { @@ -141,8 +142,8 @@ const defaultSchemas = modelName => { } }, response: { - 404: { $ref: 'MongooseApiDefErrRespSchemas404#' }, - 500: { $ref: 'MongooseApiDefErrRespSchemas500#' } + 404: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas404#' }, + 500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' } } }, routeDelete: { @@ -166,14 +167,14 @@ const defaultSchemas = modelName => { deletedCount: { type: 'number' } } }, - 404: { $ref: 'MongooseApiDefErrRespSchemas404#' }, - 500: { $ref: 'MongooseApiDefErrRespSchemas500#' } + 404: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas404#' }, + 500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' } } } }; }; -const responseSchema404 = { +const responseSchema404:ajvSchema = { $id: 'MongooseApiDefErrRespSchemas404', description: 'Not found', type: 'object', @@ -184,7 +185,7 @@ const responseSchema404 = { } }; -const responseSchema500 = { +const responseSchema500: ajvSchema = { $id: 'MongooseApiDefErrRespSchemas500', description: 'Server error', type: 'object', @@ -195,4 +196,4 @@ const responseSchema500 = { } }; -module.exports = { defaultSchemas, responseSchema404, responseSchema500 }; +export { defaultSchemas, responseSchema404, responseSchema500 }; diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 0000000..571b01f --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,116 @@ + +export type TFMAFiltersPagination = { + offset?: number; + skip?: number; + limit?: number; + page?: number; + window?: number; +}; + +export type TFMAFiletrsSort = { + sort?: string; +}; + +export type TFMAFiltersProjection = { + projection?: string; +}; + +export type TFMAFiltersPopulate = { + populate?: string; +}; + +export type TFMAFiltersSearch = { + where?: string; + search?: string; + match?: string; + filter?: string; +}; + +export type TFMAFilters = TFMAFiltersPagination & TFMAFiletrsSort & TFMAFiltersProjection & TFMAFiltersSearch & TFMAFiltersPopulate; + +export type TFMASchemaVerbs = 'routeGet' | 'routePost' | 'routePut' | 'routeDelete' | 'routePatch' | 'routeList'; + +export type TFMASchema = { + summary: string; + tags: Array; + params?: ajvSchema + query?: Omit & { properties: Partial>}; + querystring?: Omit & { properties: Partial>}; + body?: any; + response?: { + 200?: ajvSchema; + 204?: ajvSchema; + 302?: ajvSchema; + 400?: ajvSchema; + 401?: ajvSchema; + 404?: ajvSchema; + 500?: ajvSchema; + }; +}; + +export type TFMASchemas = Record; + +export type ajvType = + | 'string' + | 'number' + | 'integer' + | 'boolean' + | 'object' + | 'array' + | 'null' + | 'timestamp'; + +export type ajvProperty = { + [key: string]: ajvSchema | ajvProperties; +} + +export type ajvProperties = { + properties?: ajvProperty; +}; + +export type ajvSchema = ajvProperties & { + type: ajvType; + items?: ajvSchema[]; + $ref?: string; + description?: string; + required?: Array; + enum?: Array; + default?: any; + additionalProperties?: boolean; + oneOf?: Array; + anyOf?: Array; + allOf?: Array; + not?: ajvSchema; + title?: string; + format?: string; + minLength?: number; + maxLength?: number; + minimum?: number; + maximum?: number; + multipleOf?: number; + exclusiveMinimum?: number; + exclusiveMaximum?: number; + pattern?: string; + minItems?: number; + maxItems?: number; + uniqueItems?: boolean; + contains?: ajvSchema; + examples?: Array; + const?: any; + readOnly?: boolean; + writeOnly?: boolean; + discriminator?: string; + xml?: any; + externalDocs?: any; + deprecated?: boolean; + if?: ajvSchema; + then?: ajvSchema; + else?: ajvSchema; + contentEncoding?: string; + contentMediaType?: string; + contentSchema?: ajvSchema; + //[key: string]: any; + example?: string | number | boolean | object | Array | null; + // not sure $id goes here + $id?: string; +};