generated from senecajs/seneca-gateway
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from guhmerces/feat/inital_structure
Feat/inital structure
- Loading branch information
Showing
7 changed files
with
365 additions
and
86 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { EntityMap } from "./types"; | ||
|
||
const entities: EntityMap = { | ||
event: { | ||
actions: { | ||
load: { | ||
request: { | ||
method: "get", | ||
path: "/events/:event_id/", | ||
}, | ||
before: [ | ||
{ on:'query', field: 'attribute', set: { query: 'event_id' } } | ||
] | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export { entities } |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ActionData } from "./types" | ||
import { perform_tasks } from "./utils" | ||
|
||
function make_actions(action_data: ActionData) { | ||
const { req_fn, request, after, before } = action_data | ||
const { path } = request | ||
|
||
async function load(this:any, msg:any) { | ||
const { q } = msg | ||
|
||
const context = { | ||
query: q, | ||
} | ||
|
||
if(before) { | ||
perform_tasks(before, context) | ||
} | ||
|
||
const built_path = build_path(path, q) | ||
|
||
const res = await req_fn(built_path) | ||
|
||
const outent = this.make$(msg.ent.entity$).data$(res) | ||
|
||
if(after) { | ||
perform_tasks(after, { | ||
res, | ||
outent, | ||
...context | ||
}) | ||
} | ||
|
||
return outent | ||
} | ||
|
||
async function save(this:any, msg:any) { | ||
|
||
} | ||
|
||
function build_path(path: string, args: Record<string, any>) { | ||
const placeholders = path | ||
.split("/") | ||
.filter(p => p.match(":(.[^/]*)")) // matches against sentences like :foo | ||
|
||
placeholders.forEach(p => { | ||
const param_name = p.split(":")[1] | ||
path = path.replace(p, args[param_name] ?? p) | ||
}) | ||
|
||
return path | ||
} | ||
|
||
return { | ||
load, | ||
save | ||
} | ||
} | ||
|
||
export { make_actions } |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export async function make_request(reqFn: CallableFunction, path: string, method: string, op?: any) { | ||
try { | ||
|
||
if(op) { | ||
const res = await reqFn(path, { | ||
method, | ||
...op | ||
}) | ||
return res | ||
} | ||
|
||
const res = await reqFn(path) | ||
return res | ||
|
||
} catch (error: any) { | ||
console.log(error) | ||
// TODO: better error description | ||
throw new Error('Eventbrite Error: ' + JSON.stringify(error.parsedError)) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
type EntityMap = { | ||
[name: string]: EntDetails | ||
} | ||
|
||
type Actions = "load" | "save" | ||
|
||
type ReqDetails = { | ||
method: string | ||
path: string | ||
body_spec?: Record<string,any> | ||
} | ||
|
||
type Task = { | ||
on: keyof Context | ||
field: string | ||
set?: Set | ||
} | ||
|
||
type Set = { | ||
[key in keyof Context]: string | ||
} | ||
|
||
type Context = { | ||
outent?: any | ||
inent?: any | ||
req?: any | ||
res?: any | ||
query?: any | ||
} | ||
|
||
type ActionDetails = { | ||
request: ReqDetails | ||
after?: Task[] | ||
before?: Task[] | ||
} | ||
|
||
type EntDetails = { | ||
name?: string | ||
actions: { | ||
[action in Actions]?: ActionDetails | ||
} | ||
} | ||
|
||
type TasksTypesFn = { | ||
set: (task: Task, context: Context) => void | ||
} | ||
|
||
interface ActionData extends ActionDetails { | ||
pattern: Record<string,string> | ||
req_fn: (path:string, options?: Record<any,string>) => Promise<any> | ||
} | ||
|
||
export type { EntityMap, EntDetails, ActionData, Task, Context, TasksTypesFn } |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Context, Task, TasksTypesFn } from "./types" | ||
|
||
function perform_tasks(tasks: Task[], context: Context ) { | ||
tasks.forEach(task => { | ||
const [_, __, ...types] = Object.keys(task) | ||
|
||
types.forEach(type => { | ||
const typeFn = tasksTypes[type as keyof TasksTypesFn] | ||
|
||
if(!typeFn) { | ||
throw new Error('unable to find task of type ' + type) | ||
} | ||
|
||
typeFn(task, context) | ||
}) | ||
}) | ||
|
||
return context | ||
} | ||
|
||
function set(task: Task, context: Context) { | ||
if(!task.set) { | ||
return | ||
} | ||
|
||
const source_name = Object.keys(task.set)[0] | ||
|
||
if(!source_name) { | ||
throw new Error('A source object is required when setting a target') | ||
} | ||
|
||
const target = context[task.on] | ||
const target_field = task.field | ||
|
||
const source = context[source_name as keyof Context] | ||
const source_field: any = task.set[source_name as keyof Context] | ||
|
||
target[target_field] = source[source_field] | ||
} | ||
|
||
const tasksTypes: TasksTypesFn = { | ||
set | ||
} | ||
|
||
export { perform_tasks }; | ||
|
||
export type { | ||
Task | ||
} |
Oops, something went wrong.