From 968fae28ca8f3172a6df37197a70f995748d0079 Mon Sep 17 00:00:00 2001 From: Ashish Gurjar Date: Sun, 17 Feb 2019 00:23:27 +0530 Subject: [PATCH] functions & schema --- package.json | 2 +- .../types/array.type.ts => helpers/index.ts} | 0 src/helpers/parser.ts | 16 +++++++ src/index.ts | 3 +- src/lib/functions/index.ts | 27 +++++++++++ src/lib/impl/index.ts | 5 -- src/lib/index.ts | 5 +- src/lib/locations/base.location.ts | 45 ++++++++++++++++++ .../{schema => }/locations/body.location.ts | 4 +- .../{schema => }/locations/cookie.location.ts | 4 +- .../{schema => }/locations/header.location.ts | 4 +- src/lib/{schema => }/locations/index.ts | 1 + src/lib/locations/method.location.ts | 47 +++++++++++++++++++ .../{schema => }/locations/param.location.ts | 4 +- .../{schema => }/locations/query.location.ts | 4 +- src/lib/parse.ts | 4 -- src/lib/schema.ts | 12 +++++ src/lib/schema/index.ts | 3 -- src/lib/schema/locations/base.location.ts | 19 -------- src/lib/schema/types/any.type.ts | 18 ------- src/lib/schema/types/object.type.ts | 18 ------- .../types/base.type.ts => types/any.type.ts} | 24 ++++++---- src/lib/types/array.type.ts | 11 +++++ src/lib/{schema => }/types/index.ts | 3 +- src/lib/{schema => }/types/number.type.ts | 12 +---- src/lib/types/object.type.ts | 11 +++++ src/lib/{schema => }/types/string.type.ts | 26 +++++----- src/lib/validators/flags.ts | 9 ++++ src/lib/{stringify.ts => validators/index.ts} | 0 29 files changed, 230 insertions(+), 111 deletions(-) rename src/{lib/schema/types/array.type.ts => helpers/index.ts} (100%) create mode 100644 src/helpers/parser.ts create mode 100644 src/lib/functions/index.ts delete mode 100644 src/lib/impl/index.ts create mode 100644 src/lib/locations/base.location.ts rename src/lib/{schema => }/locations/body.location.ts (59%) rename src/lib/{schema => }/locations/cookie.location.ts (59%) rename src/lib/{schema => }/locations/header.location.ts (59%) rename src/lib/{schema => }/locations/index.ts (83%) create mode 100644 src/lib/locations/method.location.ts rename src/lib/{schema => }/locations/param.location.ts (59%) rename src/lib/{schema => }/locations/query.location.ts (59%) delete mode 100644 src/lib/parse.ts create mode 100644 src/lib/schema.ts delete mode 100644 src/lib/schema/index.ts delete mode 100644 src/lib/schema/locations/base.location.ts delete mode 100644 src/lib/schema/types/any.type.ts delete mode 100644 src/lib/schema/types/object.type.ts rename src/lib/{schema/types/base.type.ts => types/any.type.ts} (87%) create mode 100644 src/lib/types/array.type.ts rename src/lib/{schema => }/types/index.ts (59%) rename src/lib/{schema => }/types/number.type.ts (69%) create mode 100644 src/lib/types/object.type.ts rename src/lib/{schema => }/types/string.type.ts (79%) create mode 100644 src/lib/validators/flags.ts rename src/lib/{stringify.ts => validators/index.ts} (100%) diff --git a/package.json b/package.json index 7fcbdd4..541f643 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "types": "dist/index.d.ts", "repository": "https://github.com/akgurjar/express-api.git", "author": "Ashish Gurjar ", - "license": "GNU", + "license": "GPL-3.0", "private": false, "scripts": { "start": "webpack-dev-server", diff --git a/src/lib/schema/types/array.type.ts b/src/helpers/index.ts similarity index 100% rename from src/lib/schema/types/array.type.ts rename to src/helpers/index.ts diff --git a/src/helpers/parser.ts b/src/helpers/parser.ts new file mode 100644 index 0000000..8a6eda0 --- /dev/null +++ b/src/helpers/parser.ts @@ -0,0 +1,16 @@ + +/** + * @name parse + */ +export function parse() { + // TO DO + return ''; +} + +/** + * @name stringify + */ +export function stringify(): string { + // TO DO + return ''; +} diff --git a/src/index.ts b/src/index.ts index ffa044a..9d6dd32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ -export * from './lib/index'; +// export * from './lib/index'; +import { body } from './lib/index'; diff --git a/src/lib/functions/index.ts b/src/lib/functions/index.ts new file mode 100644 index 0000000..a2ca5a1 --- /dev/null +++ b/src/lib/functions/index.ts @@ -0,0 +1,27 @@ +import { + BodyLocation, + QueryLocation, + ParamLocation, + HeaderLocation, + CookieLocation, + MethodLocation, +} from '../locations/index'; + +export function body(field: string) { + return new BodyLocation(field); +} +export function query(field: string) { + return new QueryLocation(field); +} +export function param(field: string) { + return new ParamLocation(field); +} +export function header(field: string) { + return new HeaderLocation(field); +} +export function cookie(field: string) { + return new CookieLocation(field); +} +export function method(field: string) { + return new MethodLocation(field); +} diff --git a/src/lib/impl/index.ts b/src/lib/impl/index.ts deleted file mode 100644 index 4b9e806..0000000 --- a/src/lib/impl/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { BodyLocation } from '../schema/index'; - -export function body(field: string) { - return new BodyLocation(field); -} diff --git a/src/lib/index.ts b/src/lib/index.ts index c4c765c..d9c11f5 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,3 +1,4 @@ -// import { body } from './impl/index'; - +export * from './types/index'; +export * from './locations/index'; +export * from './functions/index'; diff --git a/src/lib/locations/base.location.ts b/src/lib/locations/base.location.ts new file mode 100644 index 0000000..c547bea --- /dev/null +++ b/src/lib/locations/base.location.ts @@ -0,0 +1,45 @@ +import { AnyType } from '../types/any.type'; +import { StringType } from '../types/string.type'; +import { NumberType } from '../types/number.type'; +import { ArrayType } from '../types/array.type'; +import { ObjectType } from '../types/object.type'; + +export class BaseLocation extends AnyType { + constructor(location: string, fieldNames: string[]) { + super(); + // Push field names in schema + this._schema.fieldNames.splice(0, 0, ...fieldNames); + // set location in schema + if (location) { + this._schema.locations.push(location); + } + } + /** + * @type StringType + * @description StringType + */ + string(): StringType { + return new StringType(); + } + /** + * @type NumberType + * @description NumberType + */ + number(): NumberType { + return new NumberType(); + } + /** + * @type ArrayType + * @description ArrayType + */ + array(): ArrayType { + return new ArrayType(); + } + /** + * @type ObjectType + * @description ObjectType + */ + object(): ObjectType { + return new ObjectType(); + } +} diff --git a/src/lib/schema/locations/body.location.ts b/src/lib/locations/body.location.ts similarity index 59% rename from src/lib/schema/locations/body.location.ts rename to src/lib/locations/body.location.ts index f450a61..9a12b50 100644 --- a/src/lib/schema/locations/body.location.ts +++ b/src/lib/locations/body.location.ts @@ -1,7 +1,7 @@ import { BaseLocation } from './base.location'; export class BodyLocation extends BaseLocation { - constructor(fieldName: string) { - super('body', fieldName); + constructor(...fieldNames: string[]) { + super('body', fieldNames); } } diff --git a/src/lib/schema/locations/cookie.location.ts b/src/lib/locations/cookie.location.ts similarity index 59% rename from src/lib/schema/locations/cookie.location.ts rename to src/lib/locations/cookie.location.ts index e90c783..b1aa9e1 100644 --- a/src/lib/schema/locations/cookie.location.ts +++ b/src/lib/locations/cookie.location.ts @@ -1,7 +1,7 @@ import { BaseLocation } from './base.location'; export class CookieLocation extends BaseLocation { - constructor(fieldName: string) { - super('cookie', fieldName); + constructor(...fieldNames: string[]) { + super('cookie', fieldNames); } } diff --git a/src/lib/schema/locations/header.location.ts b/src/lib/locations/header.location.ts similarity index 59% rename from src/lib/schema/locations/header.location.ts rename to src/lib/locations/header.location.ts index fda7f34..3085051 100644 --- a/src/lib/schema/locations/header.location.ts +++ b/src/lib/locations/header.location.ts @@ -1,7 +1,7 @@ import { BaseLocation } from './base.location'; export class HeaderLocation extends BaseLocation { - constructor(fieldName: string) { - super('header', fieldName); + constructor(...fieldNames: string[]) { + super('header', fieldNames); } } diff --git a/src/lib/schema/locations/index.ts b/src/lib/locations/index.ts similarity index 83% rename from src/lib/schema/locations/index.ts rename to src/lib/locations/index.ts index 7084bbb..468afaf 100644 --- a/src/lib/schema/locations/index.ts +++ b/src/lib/locations/index.ts @@ -4,3 +4,4 @@ export * from './query.location'; export * from './param.location'; export * from './header.location'; export * from './cookie.location'; +export * from './method.location'; diff --git a/src/lib/locations/method.location.ts b/src/lib/locations/method.location.ts new file mode 100644 index 0000000..3f66e60 --- /dev/null +++ b/src/lib/locations/method.location.ts @@ -0,0 +1,47 @@ +/** + * @name MethodLocation + */ + +import { BaseLocation } from './base.location'; +import { BodyLocation } from './body.location'; +import { QueryLocation } from './query.location'; +import { ParamLocation } from './param.location'; +import { HeaderLocation } from './header.location'; +import { CookieLocation } from './cookie.location'; + +export class MethodLocation extends BaseLocation { + constructor(...methods: string[]) { + super(null, []); + this._schema.methods.splice(0, 0, ...methods); + } + /** + * @param fieldNames All field names + */ + body(...fieldNames: string[]): BodyLocation { + return new BodyLocation(...fieldNames); + } + /** + * @param fieldNames All field names + */ + query(...fieldNames: string[]): QueryLocation { + return new QueryLocation(...fieldNames); + } + /** + * @param fieldNames All field names + */ + param(...fieldNames: string[]): ParamLocation { + return new ParamLocation(...fieldNames); + } + /** + * @param fieldNames All field names + */ + header(...fieldNames: string[]): HeaderLocation { + return new HeaderLocation(...fieldNames); + } + /** + * @param fieldNames All field names + */ + cookie(...fieldNames: string[]): CookieLocation { + return new CookieLocation(...fieldNames); + } +} diff --git a/src/lib/schema/locations/param.location.ts b/src/lib/locations/param.location.ts similarity index 59% rename from src/lib/schema/locations/param.location.ts rename to src/lib/locations/param.location.ts index de7cfa0..7f7aa61 100644 --- a/src/lib/schema/locations/param.location.ts +++ b/src/lib/locations/param.location.ts @@ -1,7 +1,7 @@ import { BaseLocation } from './base.location'; export class ParamLocation extends BaseLocation { - constructor(fieldName: string) { - super('param', fieldName); + constructor(...fieldNames: string[]) { + super('param', fieldNames); } } diff --git a/src/lib/schema/locations/query.location.ts b/src/lib/locations/query.location.ts similarity index 59% rename from src/lib/schema/locations/query.location.ts rename to src/lib/locations/query.location.ts index ed8b210..fdf8a13 100644 --- a/src/lib/schema/locations/query.location.ts +++ b/src/lib/locations/query.location.ts @@ -1,7 +1,7 @@ import { BaseLocation } from './base.location'; export class QueryLocation extends BaseLocation { - constructor(fieldName: string) { - super('query', fieldName); + constructor(...fieldNames: string[]) { + super('query', fieldNames); } } diff --git a/src/lib/parse.ts b/src/lib/parse.ts deleted file mode 100644 index 4924e61..0000000 --- a/src/lib/parse.ts +++ /dev/null @@ -1,4 +0,0 @@ - -export function parse() { - // -} diff --git a/src/lib/schema.ts b/src/lib/schema.ts new file mode 100644 index 0000000..1aabd98 --- /dev/null +++ b/src/lib/schema.ts @@ -0,0 +1,12 @@ + +/** + * @name Schema + * @description A schema which can be validated, stringify, parsed easily transferred over internet. + */ + +export class EpiSchema { + readonly methods: string[] = []; + readonly locations: string[] = []; + readonly fieldNames: string[] = []; + readonly flags: string[] = []; +} diff --git a/src/lib/schema/index.ts b/src/lib/schema/index.ts deleted file mode 100644 index 5f9504f..0000000 --- a/src/lib/schema/index.ts +++ /dev/null @@ -1,3 +0,0 @@ - -export * from './types/index'; -export * from './locations/index'; diff --git a/src/lib/schema/locations/base.location.ts b/src/lib/schema/locations/base.location.ts deleted file mode 100644 index 26ecc42..0000000 --- a/src/lib/schema/locations/base.location.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { AnyType } from '../types/any.type'; -import { StringType } from '../types/string.type'; -import { NumberType } from '../types/number.type'; - -export class BaseLocation { - constructor( - private _location: string, - private _fieldName: string, - ) {} - any(): AnyType { - return new AnyType(this._location, this._fieldName); - } - string(): StringType { - return new StringType(this._location, this._fieldName); - } - number(): NumberType { - return new NumberType(this._location, this._fieldName); - } -} diff --git a/src/lib/schema/types/any.type.ts b/src/lib/schema/types/any.type.ts deleted file mode 100644 index 82bad0c..0000000 --- a/src/lib/schema/types/any.type.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @name AnyType - * @type Any - * @description AnyType - */ - -import { BaseType } from './base.type'; - -export class AnyType extends BaseType { - protected _metaData: any; - protected _schema: string[]; - constructor( - protected _location: string, - protected _fieldName: string, - ) { - super(); - } -} diff --git a/src/lib/schema/types/object.type.ts b/src/lib/schema/types/object.type.ts deleted file mode 100644 index 6442982..0000000 --- a/src/lib/schema/types/object.type.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @name ObjectType - * @type Object - * @description ObjectType - */ - -import { BaseType } from './base.type'; - -export class ObjectType extends BaseType { - protected _metaData: any; - protected _schema: string[]; - constructor( - protected _location: string, - protected _fieldName: string, - ) { - super(); - } -} diff --git a/src/lib/schema/types/base.type.ts b/src/lib/types/any.type.ts similarity index 87% rename from src/lib/schema/types/base.type.ts rename to src/lib/types/any.type.ts index b76536f..e7574a8 100644 --- a/src/lib/schema/types/base.type.ts +++ b/src/lib/types/any.type.ts @@ -1,14 +1,13 @@ /** - * @name BaseType - * @type Generic - * @description BaseType + * @name AnyType + * @type Any + * @description AnyType */ -export abstract class BaseType { - protected abstract _location: string; - protected abstract _fieldName: string; - protected abstract _metaData: any; - protected abstract _schema: string[]; +import { EpiSchema } from '../schema'; + +export abstract class AnyType { + protected _schema: EpiSchema = new EpiSchema(); /** * @type Filter * @description This allow the given argument values to be truthy for filter. @@ -109,4 +108,13 @@ export abstract class BaseType { // TO DO return this; } + /** + * @type Filter + * @description bla bla bla + */ + validate() { + return (req: any, res: any, next: any) => { + next(); + }; + } } diff --git a/src/lib/types/array.type.ts b/src/lib/types/array.type.ts new file mode 100644 index 0000000..3836dfe --- /dev/null +++ b/src/lib/types/array.type.ts @@ -0,0 +1,11 @@ +/** + * @name ArrayType + * @type Array + * @description ArrayType + */ + +import { AnyType } from './any.type'; + +export class ArrayType extends AnyType { + +} diff --git a/src/lib/schema/types/index.ts b/src/lib/types/index.ts similarity index 59% rename from src/lib/schema/types/index.ts rename to src/lib/types/index.ts index 6ea5414..5f96ebe 100644 --- a/src/lib/schema/types/index.ts +++ b/src/lib/types/index.ts @@ -1,5 +1,6 @@ -export * from './base.type'; export * from './any.type'; export * from './string.type'; export * from './number.type'; +export * from './array.type'; +export * from './object.type'; diff --git a/src/lib/schema/types/number.type.ts b/src/lib/types/number.type.ts similarity index 69% rename from src/lib/schema/types/number.type.ts rename to src/lib/types/number.type.ts index 2e0d935..60bfbe3 100644 --- a/src/lib/schema/types/number.type.ts +++ b/src/lib/types/number.type.ts @@ -4,17 +4,9 @@ * @description NumberType */ -import { BaseType } from './base.type'; +import { AnyType } from './any.type'; -export class NumberType extends BaseType { - protected _metaData: any; - protected _schema: string[]; - constructor( - protected _location: string, - protected _fieldName: string, - ) { - super(); - } +export class NumberType extends AnyType { integer(convert: boolean = false): this { // TO DO return this; diff --git a/src/lib/types/object.type.ts b/src/lib/types/object.type.ts new file mode 100644 index 0000000..47e4166 --- /dev/null +++ b/src/lib/types/object.type.ts @@ -0,0 +1,11 @@ +/** + * @name ObjectType + * @type Object + * @description ObjectType + */ + +import { AnyType } from './any.type'; + +export class ObjectType extends AnyType { + +} diff --git a/src/lib/schema/types/string.type.ts b/src/lib/types/string.type.ts similarity index 79% rename from src/lib/schema/types/string.type.ts rename to src/lib/types/string.type.ts index e62efe0..f62e025 100644 --- a/src/lib/schema/types/string.type.ts +++ b/src/lib/types/string.type.ts @@ -1,25 +1,18 @@ /** * @name StringType - * @type Number + * @type String * @description StringType */ -import { BaseType } from './base.type'; +import { AnyType } from './any.type'; -export class StringType extends BaseType { - protected _metaData: any; - protected _schema: string[]; - constructor( - protected _location: string, - protected _fieldName: string, - ) { - super(); - } +export class StringType extends AnyType { /** * @type Filter */ alphanum(): this { // check for alphanum value + this._schema.flags.push('alphanum'); return this; } /** @@ -35,6 +28,7 @@ export class StringType extends BaseType { */ lowercase(convert: boolean = true): this { // TO DO + this._schema.flags.push('alphanum'); return this; } /** @@ -43,6 +37,7 @@ export class StringType extends BaseType { */ uppercase(convert: boolean = true): this { // TO DO + this._schema.flags.push('alphanum'); return this; } /** @@ -51,6 +46,7 @@ export class StringType extends BaseType { */ trim(convert: boolean = true): this { // trim value or check if value trim + this._schema.flags.push('alphanum'); return this; } /** @@ -58,6 +54,7 @@ export class StringType extends BaseType { */ trimEnd(convert: boolean = true): this { // check for alphanum value + this._schema.flags.push('alphanum'); return this; } /** @@ -65,6 +62,7 @@ export class StringType extends BaseType { */ trimStart(convert: boolean = true): this { // check for alphanum value + this._schema.flags.push('alphanum'); return this; } /** @@ -86,6 +84,7 @@ export class StringType extends BaseType { */ email(): this { // check for alphanum value + this._schema.flags.push('email'); return this; } /** @@ -93,6 +92,7 @@ export class StringType extends BaseType { */ isoDate(): this { // check for alphanum value + this._schema.flags.push('isoDate'); return this; } /** @@ -100,6 +100,7 @@ export class StringType extends BaseType { */ jwt(): this { // check for alphanum value + this._schema.flags.push('jwt'); return this; } /** @@ -107,6 +108,7 @@ export class StringType extends BaseType { */ uri(): this { // check for alphanum value + this._schema.flags.push('uri'); return this; } /** @@ -114,6 +116,7 @@ export class StringType extends BaseType { */ dataUri(): this { // check for alphanum value + this._schema.flags.push('dataUri'); return this; } /** @@ -121,6 +124,7 @@ export class StringType extends BaseType { */ ip(): this { // check for alphanum value + this._schema.flags.push('alphanum'); return this; } /** diff --git a/src/lib/validators/flags.ts b/src/lib/validators/flags.ts new file mode 100644 index 0000000..4ef048c --- /dev/null +++ b/src/lib/validators/flags.ts @@ -0,0 +1,9 @@ +/** + * @description All validator flags. + */ + +import {} from '../schema'; + +function trim() { + // +} diff --git a/src/lib/stringify.ts b/src/lib/validators/index.ts similarity index 100% rename from src/lib/stringify.ts rename to src/lib/validators/index.ts