-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/MiaadTeam/lesan
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.99
- v0.0.98
- v0.0.97
- v0.0.96
- v0.0.95
- v0.0.94
- v0.0.93
- v0.0.92
- v0.0.91
- v0.0.90
- v0.0.89
- v0.0.88
- v0.0.87
- v0.0.86
- v0.0.85
- v0.0.84
- v0.0.83
- v0.0.82
- v0.0.81
- v0.0.80
- v0.0.79
- v0.0.78
- v0.0.77
- v0.0.76
- v0.0.75
- v0.0.74
- v0.0.73
- v0.0.72
- v0.0.71
- v0.0.70
- v0.0.69
- v0.0.68
- v0.0.67
- v0.0.66
- v0.0.65
- v0.0.64
- v0.0.63
- v0.0.62
- 0.060
- 0.0.61
- 0.0.60
- 0.0.59
- 0.0.58
- 0.0.57
- 0.0.56
Showing
13 changed files
with
1,190 additions
and
538 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,167 @@ | ||
import { Struct } from "../deps.ts"; | ||
import { Body } from "../utils/mod.ts"; | ||
|
||
/** | ||
* type of ActFn | ||
* @param body - input of function is type of body | ||
* type of Body is equal to | ||
* { service?: ServiceKeys; - nameOfService | ||
* contents: "dynamic" | "static"; - type of contest | ||
* wants: { | ||
* model: string; -model that client want to execute action (name of schema) | ||
* act: string; | ||
* }; | ||
* details: Details; | ||
* } | ||
*/ | ||
export type ActFn = (body: Body) => any; | ||
|
||
/** | ||
* interface of Act is include of tow features | ||
* validator of function and fn | ||
* @interface | ||
*/ | ||
export interface Act { | ||
validator: Struct<any>; | ||
fn: ActFn; | ||
validator: Struct<any>; | ||
fn: ActFn; | ||
} | ||
|
||
/** | ||
* Acts include tow features : dynamic and static | ||
* dynamic for dynamic request and static for static request for example get static file | ||
* @example | ||
* | ||
* dynamic: { | ||
* user: { | ||
* create: { | ||
* validator: (input: any) => { | ||
* return true; | ||
* }, | ||
* fn: (input: any) => { | ||
* return input; | ||
* }, | ||
* }, | ||
* update: { | ||
* validator: (input: any) => { | ||
* return true; | ||
* }, | ||
* fn: (input: any) => { | ||
* return input; | ||
* }, | ||
* }, | ||
* }, | ||
* }, | ||
* static: { | ||
* "blogFirstPage": { | ||
* "get": { | ||
* "validator": (input: any) => { | ||
* return true; | ||
* }, | ||
* "fn": (input: any) => { | ||
* return input; | ||
* }, | ||
* }, | ||
* "set": { | ||
* "validator": (input: any) => { | ||
* return true; | ||
* }, | ||
* "fn": (input: any) => { | ||
* return input; | ||
* }, | ||
* }, | ||
* }, | ||
* }, | ||
*/ | ||
export interface Acts { | ||
dynamic: { | ||
[key: string]: { | ||
[key: string]: Act; | ||
dynamic: { | ||
[key: string]: { | ||
[key: string]: Act; | ||
}; | ||
}; | ||
}; | ||
static: { | ||
[key: string]: { | ||
[key: string]: Act; | ||
static: { | ||
[key: string]: { | ||
[key: string]: Act; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
* service inteface is include main service and functions | ||
* and also maybe include other services | ||
* @example | ||
* { | ||
* main:{ | ||
* dynamic: { | ||
* user: { | ||
* create: { | ||
* validator: (input: any) => { | ||
* return true; | ||
* }, | ||
* fn: (input: any) => { | ||
* return input; | ||
* }, | ||
* }, | ||
* static: { | ||
* "blogFirstPage": { | ||
* "get": { | ||
* "validator": (input: any) => { | ||
* return true; | ||
* }, | ||
* "fn": (input: any) => { | ||
* return input; | ||
* }, | ||
* }, | ||
* } | ||
* }, | ||
* }, | ||
* "ecommerce":"https://localhost:5050/lesan", | ||
* "blog":{ | ||
* dynamic: { | ||
* user: { | ||
* create: { | ||
* validator: (input: any) => { | ||
* return true; | ||
* }, | ||
* fn: (input: any) => { | ||
* return input; | ||
* }, | ||
* }, | ||
* } | ||
* }, | ||
* main services is type of Acts , other services maybe type of string or Act: | ||
* if type of string we get answer of req with http Request , but if type of it equal to Acts with anwer to req directly | ||
*/ | ||
export interface Services { | ||
main: Acts; | ||
[key: string]: Acts | string | undefined; | ||
main: Acts; | ||
[key: string]: Acts | string | undefined; | ||
} | ||
|
||
/** | ||
* ActInp is type of action in lesan | ||
* for set action function | ||
* @interface | ||
*/ | ||
export interface ActInp { | ||
type: "static" | "dynamic"; | ||
schema: string; | ||
actName: string; | ||
validator: Struct<any>; | ||
fn: ActFn; | ||
/** | ||
* type of action static or dynamic | ||
* when equal to static for get static file (isdb) | ||
* else for dynamic request(request to db ideed) | ||
*/ | ||
type: "static" | "dynamic"; | ||
/** | ||
* name of schema that set action for it | ||
*/ | ||
schema: string; | ||
/** | ||
* name of action | ||
*/ | ||
actName: string; | ||
/** | ||
* validator function for example for validion input date | ||
*/ | ||
validator: Struct<any>; | ||
|
||
/** | ||
* function | ||
*/ | ||
fn: ActFn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,62 @@ | ||
/// context type ---- | ||
// | ||
/** | ||
* Context Holds values and carries them in functions. | ||
* @example | ||
* { | ||
* Request: "values of Req", | ||
* user: { | ||
* "_id":1, | ||
* "name":"ali", | ||
* "lastName":"Alavi" | ||
* "role":"Admin" | ||
* } * | ||
*/ | ||
export interface Context { | ||
[key: string]: any; | ||
[key: string]: any; | ||
} | ||
|
||
/** | ||
* this function is create for define all things in local scope | ||
* and also all functions of context define in this function | ||
* @returns - return objects of all functions that define in this function | ||
*/ | ||
export const contextFns = () => { | ||
let context: Context = {}; | ||
/** | ||
* variable of context | ||
* @defaultValue | ||
* the value of context is '{}' | ||
*/ | ||
let context: Context = {}; | ||
|
||
const getContextModel = () => context; | ||
/** | ||
* @returns The contextObj | ||
*/ | ||
const getContextModel = () => context; | ||
/** | ||
* asigne all of value that we want to carry | ||
* @param con - objects of key , value | ||
* @returns nothing | ||
*/ | ||
const addContexts = (con: Context) => context = con; | ||
|
||
const addContexts = (con: Context) => context = con; | ||
/** | ||
* add values to previous values that we want to carry | ||
* @param con - objects of key , value | ||
* @returns nothing | ||
*/ | ||
const addContext = (con: Context) => context = { ...context, con }; | ||
/** | ||
* add Request to Context because the requeste may be required in later functions | ||
* @param con - request of user | ||
* @returns nothing | ||
*/ | ||
const addReqToContext = (con: Request) => context["Request"] = con; | ||
|
||
const addContext = (con: Context) => context = { ...context, con }; | ||
|
||
const addReqToContext = (con: Request) => context["Request"] = con; | ||
|
||
return { | ||
getContextModel, | ||
addContexts, | ||
addContext, | ||
addReqToContext, | ||
}; | ||
return { | ||
getContextModel, | ||
addContexts, | ||
addContext, | ||
addReqToContext, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
import { ISchema } from "./mod.ts"; | ||
import { schemaFns } from "./schema.ts"; | ||
|
||
/** | ||
* this function is create for define all things in local scope | ||
* and also all functions of relationFns define in this function | ||
* @param {@link ISchema} schemasObjs - input is all record of schemas | ||
* @returns - return objects of all functions that define in this function | ||
*/ | ||
export const relationFns = (schemasObjs: ISchema) => { | ||
const getRelation = ( | ||
name: string, | ||
relationType: "inrelation" | "outrelation", | ||
) => { | ||
const schemas = schemaFns(schemasObjs).getSchemas(); | ||
return schemas[name][relationType]; | ||
}; | ||
/** | ||
* get inerRelatrion or outerRealtion of one schema | ||
* @param name - name of schema | ||
* @param relationType - type of relation that we want (inerRelatrion or outrelation) | ||
*/ | ||
const getRelation = ( | ||
name: string, | ||
relationType: "inrelation" | "outrelation", | ||
) => { | ||
const schemas = schemaFns(schemasObjs).getSchemas(); | ||
return schemas[name][relationType]; | ||
}; | ||
|
||
return { | ||
getRelation, | ||
}; | ||
return { | ||
getRelation, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,55 @@ | ||
import { Struct } from "../deps.ts"; | ||
|
||
/** | ||
* PureModel is interface of pure feature, | ||
* pure feature is an intrinsic feature of an schema. Which are embedded in other schemas | ||
* @public | ||
*/ | ||
export interface PureModel { | ||
[key: string]: Struct<any>; | ||
[key: string]: Struct<any>; | ||
} | ||
|
||
/** | ||
* if schema has relation with other schema and in SQL that we keep foreign key. | ||
* store in InRelation feature | ||
* @public | ||
*/ | ||
export interface InRelation { | ||
schemaName: string; | ||
type: "one" | "many"; | ||
/** | ||
* name of schema that this schema has relation with | ||
*/ | ||
schemaName: string; | ||
/** | ||
* type of relation if equal to one: this schema record one object from other schema else | ||
* this schema record array of object from other schema | ||
*/ | ||
type: "one" | "many"; | ||
} | ||
|
||
/** | ||
* if schema has relation with other schema and in SQL that we dont keep foriegn key. | ||
* store in OutRelation feature | ||
* and usually the number of it greater thant of 50 | ||
* @public | ||
*/ | ||
export interface OutRelation { | ||
schemaName: string; | ||
number: number; | ||
sort: { field: string; order: "asc" | "desc" }; | ||
/** | ||
* name of schema that this schema has relation with | ||
*/ | ||
schemaName: string; | ||
/** | ||
* number of value that we want to keep | ||
*/ | ||
number: number; | ||
/** | ||
* sort : {field , order} - field of sort , and order of sort | ||
*/ | ||
sort: { field: string; order: "asc" | "desc" }; | ||
} | ||
|
||
/** | ||
* this model includes :pure feature , inrelation feature and outrelation feacture | ||
* @public | ||
*/ | ||
export interface Model { | ||
pure: PureModel; | ||
inrelation: Record<string, InRelation>; | ||
outrelation: Record<string, OutRelation>; | ||
pure: PureModel; | ||
inrelation: Record<string, InRelation>; | ||
outrelation: Record<string, OutRelation>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,115 @@ | ||
import { throwError } from "./mod.ts"; | ||
|
||
/** | ||
* details of input is include set , get | ||
* @public | ||
*/ | ||
export interface Details { | ||
set: Record<string, any>; | ||
get: Record<string, any>; | ||
/** | ||
* set of query | ||
*/ | ||
set: Record<string, any>; | ||
/** | ||
* get pf query | ||
* What the client wants to return | ||
*/ | ||
get: Record<string, any>; | ||
} | ||
|
||
/** | ||
* interface is type of input of Actions | ||
* @public | ||
*/ | ||
export interface Body { | ||
service?: string; | ||
contents: "dynamic" | "static"; | ||
wants: { | ||
model: string; | ||
act: string; | ||
}; | ||
details: Details; | ||
/** | ||
* name of service | ||
* "main" | "blog" | "ecommerce" | ||
*/ | ||
service?: string; | ||
/** | ||
* type of contents : dynamic | static | ||
*/ | ||
contents: "dynamic" | "static"; | ||
/** | ||
* model : schema name that client wants | ||
* act : name of Actions | ||
*/ | ||
wants: { | ||
model: string; | ||
act: string; | ||
}; | ||
/** | ||
* details of request set and get | ||
*/ | ||
details: Details; | ||
} | ||
|
||
const decodeBody = async (req: Request): Promise<Body> => { | ||
/** | ||
* @function | ||
* decode body of request when content type is application/json | ||
*/ | ||
const decodeJsonBody = async () => | ||
req.body | ||
? JSON.parse(await req.text()) as Body | ||
: throwError("Your request body is incorrect"); | ||
/** | ||
* @function | ||
* decode body of request when content type is application/json | ||
*/ | ||
const decodeJsonBody = async () => | ||
req.body | ||
? JSON.parse(await req.text()) as Body | ||
: throwError("Your request body is incorrect"); | ||
|
||
/** | ||
* @function | ||
* decode all files and funql body when type of content type is multipart/form-data | ||
* @remarks it puts all of files in set of details of request body | ||
* @return parsed body with uploaded files | ||
* @example we recommend to use postman or other client lib fot handling boundary field and other fields | ||
*/ | ||
const decodeMultiPartBody = async () => { | ||
// finds boundary of form data | ||
// const boundary = contentType.match(/boundary=([^\s]+)/)?.[1]; | ||
/** | ||
* @function | ||
* decode all files and funql body when type of content type is multipart/form-data | ||
* @remarks it puts all of files in set of details of request body | ||
* @return parsed body with uploaded files | ||
* @example we recommend to use postman or other client lib fot handling boundary field and other fields | ||
*/ | ||
const decodeMultiPartBody = async () => { | ||
// finds boundary of form data | ||
// const boundary = contentType.match(/boundary=([^\s]+)/)?.[1]; | ||
|
||
const getFileFormData: () => Promise<Body> = async () => { | ||
const fd = await req.formData(); | ||
// for (const f of fd.entries()) { | ||
// if (!(f[1] instanceof File)) { | ||
// continue; | ||
// } | ||
// const fileData = new Uint8Array(await f[1].arrayBuffer()); | ||
// await Deno.writeFile("./files/" + f[1].name, fileData); | ||
// } | ||
const returnBody: (body: string) => Body = (body) => { | ||
const parsedBody = JSON.parse(body) as Body; | ||
parsedBody && | ||
parsedBody.details && | ||
parsedBody.details.set && | ||
(parsedBody.details.set = { | ||
...parsedBody.details.set, | ||
formData: fd, | ||
}); | ||
return parsedBody; | ||
}; | ||
const getFileFormData: () => Promise<Body> = async () => { | ||
const fd = await req.formData(); | ||
// for (const f of fd.entries()) { | ||
// if (!(f[1] instanceof File)) { | ||
// continue; | ||
// } | ||
// const fileData = new Uint8Array(await f[1].arrayBuffer()); | ||
// await Deno.writeFile("./files/" + f[1].name, fileData); | ||
// } | ||
const returnBody: (body: string) => Body = (body) => { | ||
const parsedBody = JSON.parse(body) as Body; | ||
parsedBody | ||
&& parsedBody.details | ||
&& parsedBody.details.set | ||
&& (parsedBody.details.set = { | ||
...parsedBody.details.set, | ||
formData: fd, | ||
}); | ||
return parsedBody; | ||
}; | ||
|
||
const body = fd.get("lesan-body") ? fd.get("lesan-body") as string : "{}"; | ||
const body = fd.get("lesan-body") ? fd.get("lesan-body") as string : "{}"; | ||
|
||
return body | ||
? returnBody(body) | ||
: throwError("somthing wrong with your file"); | ||
}; | ||
return body | ||
? returnBody(body) | ||
: throwError("somthing wrong with your file"); | ||
}; | ||
|
||
return req.body | ||
? await getFileFormData() | ||
: throwError("Your body is incorrect"); | ||
}; | ||
return req.body | ||
? await getFileFormData() | ||
: throwError("Your body is incorrect"); | ||
}; | ||
|
||
const contentType = req.headers.get("content-type") || ""; | ||
return contentType.includes("application/json") | ||
? await decodeJsonBody() | ||
: contentType.includes("multipart/form-data") | ||
? await decodeMultiPartBody() | ||
: throwError("content type is not correct"); | ||
const contentType = req.headers.get("content-type") || ""; | ||
return contentType.includes("application/json") | ||
? await decodeJsonBody() | ||
: contentType.includes("multipart/form-data") | ||
? await decodeMultiPartBody() | ||
: throwError("content type is not correct"); | ||
}; | ||
|
||
export const parsBody = async (req: Request, port: number) => { | ||
const parsedBody = await decodeBody(req); | ||
const url = req.url.split(`${port}`)[1]; | ||
const parsedBody = await decodeBody(req); | ||
const url = req.url.split(`${port}`)[1]; | ||
|
||
return req.method === "POST" && url === "/lesan" | ||
? parsedBody | ||
: throwError("you most send a post request to /lesan url"); | ||
return req.method === "POST" && url === "/lesan" | ||
? parsedBody | ||
: throwError("you most send a post request to /lesan url"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* get message and throw error | ||
* @param msg - message of error | ||
* @returns throwError with input message | ||
*/ | ||
export const throwError = (msg?: string) => { | ||
throw new Error(msg); | ||
throw new Error(msg); | ||
}; |