Skip to content

Commit

Permalink
Add types for DefaultSchemas and converted DefaultSchemas to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoBruni committed Feb 13, 2024
1 parent 7af49e3 commit 7c54d53
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/libs/DefaultSchemas.js → src/libs/DefaultSchemas.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,7 +12,8 @@ const defaultSchemas = modelName => {
type: 'string',
description: 'Unique identifier of ' + modelName
}
}
},

},
querystring: {
type: 'object',
Expand All @@ -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: {
Expand All @@ -41,7 +42,7 @@ const defaultSchemas = modelName => {
}
},
response: {
500: { $ref: 'MongooseApiDefErrRespSchemas500#' }
500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' }
}
},
routeList: {
Expand Down Expand Up @@ -90,7 +91,7 @@ const defaultSchemas = modelName => {
}
},
response: {
500: { $ref: 'MongooseApiDefErrRespSchemas500#' }
500: { type: 'object', $ref: 'MongooseApiDefErrRespSchemas500#' }
}
},
routePut: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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',
Expand All @@ -184,7 +185,7 @@ const responseSchema404 = {
}
};

const responseSchema500 = {
const responseSchema500: ajvSchema = {
$id: 'MongooseApiDefErrRespSchemas500',
description: 'Server error',
type: 'object',
Expand All @@ -195,4 +196,4 @@ const responseSchema500 = {
}
};

module.exports = { defaultSchemas, responseSchema404, responseSchema500 };
export { defaultSchemas, responseSchema404, responseSchema500 };
116 changes: 116 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
params?: ajvSchema
query?: Omit<ajvSchema, 'properties'> & { properties: Partial<Record<keyof TFMAFilters, ajvSchema>>};
querystring?: Omit<ajvSchema, 'properties'> & { properties: Partial<Record<keyof TFMAFilters, ajvSchema>>};
body?: any;
response?: {
200?: ajvSchema;
204?: ajvSchema;
302?: ajvSchema;
400?: ajvSchema;
401?: ajvSchema;
404?: ajvSchema;
500?: ajvSchema;
};
};

export type TFMASchemas = Record<TFMASchemaVerbs, TFMASchema>;

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<string>;
enum?: Array<string>;
default?: any;
additionalProperties?: boolean;
oneOf?: Array<ajvSchema>;
anyOf?: Array<ajvSchema>;
allOf?: Array<ajvSchema>;
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<any>;
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<any> | null;
// not sure $id goes here
$id?: string;
};

0 comments on commit 7c54d53

Please sign in to comment.