Skip to content

Commit

Permalink
Showing 21 changed files with 733 additions and 562 deletions.
265 changes: 126 additions & 139 deletions src/acts/acts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Struct } from "https://deno.land/x/lestruct/mod.ts";
import { SchemasKey } from "../models/mod.ts";
import { Body, throwError } from "../utils/mod.ts";
import { throwError } from "../utils/mod.ts";
import { Act, ActInp, Acts, Services } from "./types.ts";

// const actsSample = {
//
@@ -46,210 +45,198 @@ import { Body, throwError } from "../utils/mod.ts";
// },
// };

export type ActFn = (body: Body) => any;
export const acts = (acts: Services) => {
type ServiceKeys = keyof typeof acts;

export interface Act {
validator: Struct<any>;
fn: ActFn;
}

export interface Acts {
dynamic: {
[key: string]: {
[key: string]: Act;
};
};
static: {
[key: string]: {
[key: string]: Act;
};
};
}

export interface Services {
main: Acts;
[key: string]: Acts | string | undefined;
}

const acts: Services = {
main: {
dynamic: {},
static: {},
},
};

export interface ActInp {
type: "static" | "dynamic";
schema: SchemasKey;
actName: string;
validator: Struct<any>;
fn: ActFn;
}

export const setAct: (actInp: ActInp) => void = (
const setAct: (actInp: ActInp) => void = (
{ type, schema, actName, validator, fn },
) => {
) => {
if (!acts.main[type]) {
throw new Error(`Invalid type: ${type}`);
throw new Error(`Invalid type: ${type}`);
}
if (!acts.main[type][schema]) {
acts.main[type][schema] = {};
acts.main[type][schema] = {};
}
acts.main[type][schema][actName] = {
validator,
fn,
validator,
fn,
};
};
export type ServiceKeys = keyof typeof acts;

export const getDynamicActs = (serviceName?: ServiceKeys) => {
return (serviceName && acts[serviceName]
&& (typeof acts[serviceName] !== "string"))
? (acts[serviceName] as Acts).dynamic
: acts.main.dynamic;
};

export const getStaticActs = (serviceName?: ServiceKeys) => {
return (serviceName && acts[serviceName]
&& (typeof acts[serviceName] !== "string"))
? (acts[serviceName] as Acts).static
: acts.main.static;
};

export const getStaticKeys = (serviceName?: ServiceKeys) => {
return (serviceName && acts[serviceName]
&& (typeof acts[serviceName] !== "string"))
? Object.keys((acts[serviceName] as Acts).static)
: (serviceName === "main")
? Object.keys(acts.main.static)
: throwError(`serviceName not valid : ${serviceName}`);
};

// TODO : check if acts[serviceName] === "string" should throw an error
export const getDynamicKeys = (serviceName: ServiceKeys) => {
return (serviceName && acts[serviceName]
&& (typeof acts[serviceName] !== "string"))
? Object.keys((acts[serviceName] as Acts).dynamic)
: (serviceName === "main")
? Object.keys(acts.main.dynamic)
: throwError(`serviceName not valid : ${serviceName}`);
};

export const getServiceKeys = () => Object.keys(acts);

export const getSchemaDynamicActs: (
schema: SchemasKey,
) => { [key: string]: Act } = (
};

const getDynamicActs = (serviceName?: ServiceKeys) => {
return (serviceName && acts[serviceName] &&
(typeof acts[serviceName] !== "string"))
? (acts[serviceName] as Acts).dynamic
: acts.main.dynamic;
};

const getStaticActs = (serviceName?: ServiceKeys) => {
return (serviceName && acts[serviceName] &&
(typeof acts[serviceName] !== "string"))
? (acts[serviceName] as Acts).static
: acts.main.static;
};

const getStaticKeys = (serviceName?: ServiceKeys) => {
return (serviceName && acts[serviceName] &&
(typeof acts[serviceName] !== "string"))
? Object.keys((acts[serviceName] as Acts).static)
: (serviceName === "main")
? Object.keys(acts.main.static)
: throwError(`serviceName not valid : ${serviceName}`);
};

// TODO : check if acts[serviceName] === "string" should throw an error
const getDynamicKeys = (serviceName: ServiceKeys) => {
return (serviceName && acts[serviceName] &&
(typeof acts[serviceName] !== "string"))
? Object.keys((acts[serviceName] as Acts).dynamic)
: (serviceName === "main")
? Object.keys(acts.main.dynamic)
: throwError(`serviceName not valid : ${serviceName}`);
};

const getServiceKeys = () => Object.keys(acts);

const getSchemaDynamicActs: (
schema: string,
) => { [key: string]: Act } = (
schema,
) => {
) => {
if (!acts.main.dynamic[schema]) {
throw new Error(`Invalid schema: ${schema}`);
throw new Error(`Invalid schema: ${schema}`);
}
return acts.main.dynamic[schema];
};
};

export const getSchemaStaticActs: (schema: string) => { [key: string]: Act } = (
const getSchemaStaticActs: (schema: string) => { [key: string]: Act } = (
schema,
) => {
) => {
if (!acts.main.static[schema]) {
throw new Error(`Invalid schema: ${schema}`);
throw new Error(`Invalid schema: ${schema}`);
}
return acts.main.static[schema];
};
};

export const getDynamicAct: (
schema: SchemasKey,
const getDynamicAct: (
schema: string,
actName: string,
) => Act = (schema, actName) => {
) => Act = (schema, actName) => {
if (!acts.main.dynamic[schema]) {
throw new Error(`Invalid schema: ${schema}`);
throw new Error(`Invalid schema: ${schema}`);
}
if (!acts.main.dynamic[schema][actName]) {
throw new Error(`Invalid actName: ${actName}`);
throw new Error(`Invalid actName: ${actName}`);
}
return acts.main.dynamic[schema][actName];
};
};

export const getStaticAct: (
const getStaticAct: (
schema: string,
actName: string,
) => Act = (schema, actName) => {
) => Act = (schema, actName) => {
if (!acts.main.static[schema]) {
throw new Error(`Invalid actName: ${actName}`);
throw new Error(`Invalid actName: ${actName}`);
}
if (!acts.main.static[schema][actName]) {
throw new Error(`Invalid actName: ${actName}`);
throw new Error(`Invalid actName: ${actName}`);
}
return acts.main.static[schema][actName];
};
};

export const getActs = (type: "static" | "dynamic", schema: string) => {
const getActs = (type: "static" | "dynamic", schema: string) => {
if (!acts.main[type]) {
throw new Error(
`Invalid action type: ${type} it just include dynamic and static`,
);
throw new Error(
`Invalid action type: ${type} it just include dynamic and static`,
);
}
if (!acts.main[type][schema]) {
throw new Error(`Invalid schema: ${schema}`);
throw new Error(`Invalid schema: ${schema}`);
}
return acts.main[type][schema];
};
};

export const getActsKeys = (
const getActsKeys = (
service: ServiceKeys,
type: "static" | "dynamic",
schema: string,
) => {
) => {
if (!acts[service] && typeof acts[service] === "string") {
throw new Error(
`Invalid service name: ${service} `,
);
throw new Error(
`Invalid service name: ${service} `,
);
}
if (!(acts[service] as Acts)[type]) {
throw new Error(
`Invalid action type: ${type} it just include dynamic and static`,
);
throw new Error(
`Invalid action type: ${type} it just include dynamic and static`,
);
}
if (!(acts[service] as Acts)[type][schema]) {
throw new Error(`Invalid schema: ${schema}`);
throw new Error(`Invalid schema: ${schema}`);
}
return Object.keys((acts[service] as Acts)[type][schema]);
};
};

export const getAct = (
const getAct = (
service: ServiceKeys,
type: "static" | "dynamic",
schema: string,
actName: string,
) => {
) => {
if (!acts[service] && typeof acts[service] === "string") {
throw new Error(
`Invalid service name: ${service} `,
);
throw new Error(
`Invalid service name: ${service} `,
);
}
if (!(acts[service] as Acts)[type]) {
throw new Error(
`Invalid action type: ${type} it just include dynamic and static`,
);
throw new Error(
`Invalid action type: ${type} it just include dynamic and static`,
);
}
if (!(acts[service] as Acts)[type][schema]) {
throw new Error(`Invalid schema: ${schema}`);
throw new Error(`Invalid schema: ${schema}`);
}
if (!(acts[service] as Acts)[type][schema][actName]) {
throw new Error(`Invalid action name: ${actName}`);
throw new Error(`Invalid action name: ${actName}`);
}
return (acts[service] as Acts)[type][schema][actName];
};
};

const getAtcsWithServices = () => acts;

export const getAtcsWithServices = () => acts;
const getMainActs = () => acts.main;

export const setService: (serviceName: string, service: Acts | string) => void = (serviceName, service) => {
const setService: (serviceName: string, service: Acts | string) => void = (
serviceName,
service,
) => {
acts[serviceName] = service;
};
};

export const getService: (serviceName: ServiceKeys) => void = (serviceName) => {
const getService: (serviceName: ServiceKeys) => void = (serviceName) => {
if (!acts[serviceName]) {
throw new Error(`Invalid serviceName: ${serviceName}`);
throw new Error(`Invalid serviceName: ${serviceName}`);
}
return acts[serviceName];
};

return {
setAct,
getDynamicActs,
getStaticActs,
getStaticKeys,
getDynamicKeys,
getServiceKeys,
getSchemaDynamicActs,
getSchemaStaticActs,
getDynamicAct,
getStaticAct,
getActs,
getActsKeys,
getAct,
getAtcsWithServices,
getMainActs,
setService,
getService,
};
};
6 changes: 6 additions & 0 deletions src/acts/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// import { acts } from "./acts.ts";

export * from "./acts.ts";
export * from "./types.ts";

// const generatedServices = acts().getAtcsWithServices();
// export type ServiceKeys = keyof typeof generatedServices;
35 changes: 35 additions & 0 deletions src/acts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Struct } from "../deps.ts";
import { Body } from "../utils/mod.ts";

export type ActFn = (body: Body) => any;

export interface Act {
validator: Struct<any>;
fn: ActFn;
}

export interface Acts {
dynamic: {
[key: string]: {
[key: string]: Act;
};
};
static: {
[key: string]: {
[key: string]: Act;
};
};
}

export interface Services {
main: Acts;
[key: string]: Acts | string | undefined;
}

export interface ActInp {
type: "static" | "dynamic";
schema: string;
actName: string;
validator: Struct<any>;
fn: ActFn;
}
1 change: 1 addition & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { ensureDir } from "https://deno.land/[email protected]/fs/mod.ts";
export * from "https://deno.land/x/[email protected]/mod.ts";
export * from "https://deno.land/x/[email protected]/mod.ts";
Loading

0 comments on commit c89f626

Please sign in to comment.