Skip to content

Commit

Permalink
functions & schema
Browse files Browse the repository at this point in the history
  • Loading branch information
akgurjar committed Feb 16, 2019
1 parent 9870229 commit 968fae2
Show file tree
Hide file tree
Showing 29 changed files with 230 additions and 111 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"types": "dist/index.d.ts",
"repository": "https://github.com/akgurjar/express-api.git",
"author": "Ashish Gurjar <[email protected]>",
"license": "GNU",
"license": "GPL-3.0",
"private": false,
"scripts": {
"start": "webpack-dev-server",
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

/**
* @name parse
*/
export function parse() {
// TO DO
return '';
}

/**
* @name stringify
*/
export function stringify(): string {
// TO DO
return '';
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

export * from './lib/index';
// export * from './lib/index';
import { body } from './lib/index';
27 changes: 27 additions & 0 deletions src/lib/functions/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 0 additions & 5 deletions src/lib/impl/index.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// import { body } from './impl/index';

export * from './types/index';
export * from './locations/index';
export * from './functions/index';
45 changes: 45 additions & 0 deletions src/lib/locations/base.location.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './query.location';
export * from './param.location';
export * from './header.location';
export * from './cookie.location';
export * from './method.location';
47 changes: 47 additions & 0 deletions src/lib/locations/method.location.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 0 additions & 4 deletions src/lib/parse.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/lib/schema.ts
Original file line number Diff line number Diff line change
@@ -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[] = [];
}
3 changes: 0 additions & 3 deletions src/lib/schema/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/lib/schema/locations/base.location.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/lib/schema/types/any.type.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/lib/schema/types/object.type.ts

This file was deleted.

24 changes: 16 additions & 8 deletions src/lib/schema/types/base.type.ts → src/lib/types/any.type.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/**
* @name BaseType
* @type Generic
* @description BaseType
* @name AnyType
* @type Any
* @description AnyType
*/

export abstract class BaseType<T> {
protected abstract _location: string;
protected abstract _fieldName: string;
protected abstract _metaData: any;
protected abstract _schema: string[];
import { EpiSchema } from '../schema';

export abstract class AnyType<T> {
protected _schema: EpiSchema = new EpiSchema();
/**
* @type Filter
* @description This allow the given argument values to be truthy for filter.
Expand Down Expand Up @@ -109,4 +108,13 @@ export abstract class BaseType<T> {
// TO DO
return this;
}
/**
* @type Filter
* @description bla bla bla
*/
validate() {
return (req: any, res: any, next: any) => {
next();
};
}
}
11 changes: 11 additions & 0 deletions src/lib/types/array.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @name ArrayType
* @type Array
* @description ArrayType
*/

import { AnyType } from './any.type';

export class ArrayType extends AnyType<string> {

}
3 changes: 2 additions & 1 deletion src/lib/schema/types/index.ts → src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@
* @description NumberType
*/

import { BaseType } from './base.type';
import { AnyType } from './any.type';

export class NumberType extends BaseType<number> {
protected _metaData: any;
protected _schema: string[];
constructor(
protected _location: string,
protected _fieldName: string,
) {
super();
}
export class NumberType extends AnyType<number> {
integer(convert: boolean = false): this {
// TO DO
return this;
Expand Down
11 changes: 11 additions & 0 deletions src/lib/types/object.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @name ObjectType
* @type Object
* @description ObjectType
*/

import { AnyType } from './any.type';

export class ObjectType extends AnyType<string> {

}
Loading

0 comments on commit 968fae2

Please sign in to comment.