Skip to content

Commit

Permalink
Merge pull request #18 from guhmerces/refactor/prepare_ents
Browse files Browse the repository at this point in the history
Refactor/prepare ents
  • Loading branch information
rjrodger authored Aug 8, 2022
2 parents 9c9c0fb + d745c84 commit 0502f4f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 38 deletions.
67 changes: 33 additions & 34 deletions src/github-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Octokit } from '@octokit/rest'
import { make_actions } from './cmd-handlers'
import { ents } from './entities'
import { ActionData, EntityMap } from './types'
import { EntData, EntityMap } from './types'

type GithubProviderOptions = {}

Expand All @@ -30,62 +30,61 @@ function GithubProvider(this: any, _options: any) {
.message('sys:provider,provider:github,get:info', get_info)

function add_actions() {
const actions = prepare_actions(ents)
const entities = prepare_ents(ents)

for (const action of actions) {
switch (action.pattern.cmd) {
case 'load':
seneca.message(action.pattern, make_load(action))
break

case 'save':
seneca.message(action.pattern, make_save(action))
break
}
for (const ent of entities) {
seneca.message(ent.patterns.load, (make_load(ent) || unknown_cmd))
seneca.message(ent.patterns.save, (make_save(ent) || unknown_cmd))
}
}

function make_load(action: ActionData) {
function make_load(ent: EntData) {
if(!ent.load) return false
return make_actions(
action.sdk_params,
action.action_details,
ent.load.sdk_params,
ent.load.action_details,
sdk
)['load']
).load
}

function make_save(action: ActionData) {
function make_save(ent: EntData) {
if(!ent.save) return false
return make_actions(
action.sdk_params,
action.action_details,
ent.save.sdk_params,
ent.save.action_details,
sdk
)['save']
).save
}

function prepare_actions(entities: EntityMap): Array<ActionData> {
const actions_data = []
async function unknown_cmd(this: any, msg: any) {
throw new Error(`undefined action: ${msg.cmd}, entity: ${msg.ent.entity$}`)
}

function prepare_ents(entities: EntityMap): Array<EntData> {
const ents_data: EntData[] = []

for (const [ent_name, data] of Object.entries(entities)) {
const { actions } = data
data.name = ent_name
const ent_data:any = {}

const common = { name: ent_name, zone: 'provider', base: 'github', role: 'entity'}
ent_data.patterns = {
load: {cmd: 'load', ...common},
save: {cmd: 'save', ...common}
}

for (const [action_name, action_details] of Object.entries(actions)) {
const pattern = {
name: ent_name,
cmd: action_name,
zone: 'provider',
base: 'github',
role: 'entity',
}

actions_data.push({
pattern,
ent_data[action_name] = {
sdk_params: data.sdk,
action_details,
})
}
}

ents_data.push(ent_data)
}

return actions_data
return ents_data
}

async function get_info(this: any, _msg: any) {
Expand Down
16 changes: 12 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ type ActionDetails = {
}

type ActionData = {
sdk_params: SdkParams
action_details: ActionDetails
pattern: Record<string, any>
[action in ActionType]: {
sdk_params: SdkParams
action_details: ActionDetails
}
}

interface EntData extends ActionData {
patterns: {
load: Record<string,any>
save: Record<string,any>
}
}

type Entity = { [key: string]: any }
Expand Down Expand Up @@ -76,4 +84,4 @@ type GithubRestEndpoints =
| "teams"
| "users"

export type { ActionType, ActionDetails, EntityMap, Entity, ActionData, SdkParams, Task, Context, TasksTypesFn }
export type { ActionType, ActionDetails, EntityMap, Entity, ActionData, SdkParams, Task, Context, TasksTypesFn, EntData }
22 changes: 22 additions & 0 deletions test/github-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ describe("github-entities-load", () => {
})
})

describe('undefined-action', () => {
test('throws-error-when-a-undefined-action-is-called', async() => {
const seneca = Seneca({ legacy: false })
.test()
.use("promisify")
.use("entity")
.use("provider", provider_options)
.use(GithubProvider)

let res_data = await seneca.entity("provider/github/" + 'license').load$({
license: 'mit'
})

try {
// there is no defined "save" action for license entity
const x = await res_data.save$()
} catch (e) {
expect(e.message).toBe('undefined action: save, entity: provider/github/license');
}
})
})

describe("github-entities-save", () => {
Object.keys(saves).forEach(ent_name => {
let test_data = saves[ent_name]
Expand Down

0 comments on commit 0502f4f

Please sign in to comment.