@voxgig/sdkgen-infrapack 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.sdk/model/target/seneca-provider.aon +120 -0
- package/.sdk/src/cmp/seneca-provider/Extras_seneca-provider.ts +4362 -0
- package/.sdk/src/cmp/seneca-provider/Gitignore_seneca-provider.ts +161 -0
- package/.sdk/src/cmp/seneca-provider/Main_seneca-provider.ts +1295 -0
- package/.sdk/tm/seneca-provider/CODE_OF_CONDUCT.md +132 -0
- package/.sdk/tm/seneca-provider/LICENSE +21 -0
- package/.sdk/tm/seneca-provider/Makefile +15 -0
- package/.sdk/tm/seneca-provider/src/tsconfig.json +18 -0
- package/.sdk/tm/seneca-provider/test/tsconfig.json +16 -0
- package/.sdk/tm/seneca-provider/tsfmt.json +3 -0
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/package.json +98 -0
- package/sdkgen-package.json +18 -0
|
@@ -0,0 +1,4362 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cmp, each,
|
|
3
|
+
File, Content, Folder,
|
|
4
|
+
jsKey, jsProp,
|
|
5
|
+
pointSegments,
|
|
6
|
+
} from '@voxgig/sdkgen'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// The rest of the seneca-provider package: its test suite, CI workflow and
|
|
10
|
+
// README. Split out of Main only for size — everything here is driven by the
|
|
11
|
+
// same `provider` shape Main builds from the model.
|
|
12
|
+
//
|
|
13
|
+
// The tests are the reason this target is worth generating at all. A provider
|
|
14
|
+
// is thin, and the thin part is exactly where the mistakes are: a cmd that
|
|
15
|
+
// forgets a parent path param, an entity that comes back under the wrong
|
|
16
|
+
// canon, a 404 that should have been `null` and instead threw. All three are
|
|
17
|
+
// checked below, offline, against the SDK's own mock transport — so a
|
|
18
|
+
// generated provider is verified without a server.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
// Does this entity's load op have a real identifying param (path or
|
|
22
|
+
// required query), e.g. GET /result?trace_id=? A paramless GET has none.
|
|
23
|
+
function loadHasKey(ent: any): boolean {
|
|
24
|
+
const point = (ent.op && ent.op.load && ent.op.load.points || [])[0]
|
|
25
|
+
if (null == point) return false
|
|
26
|
+
// apidef states which segments are variables (its ADR-003) — no brace test.
|
|
27
|
+
const hasPathParam = pointSegments(point).some((seg: any) => null != seg.var)
|
|
28
|
+
const hasQueryParam = (point.args && point.args.query || [])
|
|
29
|
+
.some((q: any) => false !== q.reqd)
|
|
30
|
+
return hasPathParam || hasQueryParam
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// The name of the entity a parent path param addresses, or '' when the model
|
|
35
|
+
// has none of that name.
|
|
36
|
+
//
|
|
37
|
+
// From `e.parentOf`, which Main derives PER KEY. `e.parentEntity` describes
|
|
38
|
+
// only the FIRST parent, so an entity nested two levels deep had every one of
|
|
39
|
+
// its parents resolved to the innermost one — addressing the wrong record, or
|
|
40
|
+
// none.
|
|
41
|
+
function parentName(e: any, key: string): string {
|
|
42
|
+
const byKey = (e.parentOf || {})[key]
|
|
43
|
+
if (null != byKey && '' !== byKey) {
|
|
44
|
+
return String(byKey)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const f = (e.fields || []).find((f: any) => f.name === key)
|
|
48
|
+
return (f && f.parentEntity) || ''
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// The seeded id of the record a parent path param points at.
|
|
53
|
+
function parentSeed(e: any, key: string): string {
|
|
54
|
+
const pe = parentName(e, key)
|
|
55
|
+
|
|
56
|
+
// A key naming no entity in the model still has to seed SOMETHING the
|
|
57
|
+
// guard accepts; strip the `_id` suffix and use that.
|
|
58
|
+
return '' !== pe ? `${pe}0` : `${key.replace(/_id$/, '')}0`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
// `key: 'value', ` pairs for an entity's parent path params, ready to splice
|
|
63
|
+
// into an object literal. Empty for a top-level entity, so the same emitter
|
|
64
|
+
// serves both.
|
|
65
|
+
//
|
|
66
|
+
// OFFLINE the value is the seeded parent id, which exists because the seed put
|
|
67
|
+
// it there. LIVE it is a local VARIABLE, emitted as ES shorthand: a real server
|
|
68
|
+
// holds whatever records it holds, and a fixture id written into a live test is
|
|
69
|
+
// a 404 waiting to happen. That is not hypothetical — seeding the live nested
|
|
70
|
+
// create is exactly how the first version of this failed, with
|
|
71
|
+
// `create: request: 404` against a parent that only ever existed in the mock.
|
|
72
|
+
function parentPairs(e: any, live: boolean): string {
|
|
73
|
+
return e.parents
|
|
74
|
+
.map((p: string) => live ? `${p}, ` : `${p}: '${parentSeed(e, p)}', `)
|
|
75
|
+
.join('')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
// The entity a parent path param addresses, or null when the model has none of
|
|
80
|
+
// that name.
|
|
81
|
+
function parentEntityFor(provider: any, e: any, key: string): any {
|
|
82
|
+
const name = parentName(e, key)
|
|
83
|
+
|
|
84
|
+
return '' === name ? null :
|
|
85
|
+
provider.entities.find((pe: any) => pe.name === name) || null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
// Can a LIVE round-trip get hold of this entity's parent ids at all? Every
|
|
90
|
+
// parent key must name an entity in the model, and that entity must be
|
|
91
|
+
// listable — otherwise there is no honest way to obtain an id the server will
|
|
92
|
+
// accept, and the test is not emitted rather than emitted and skipped.
|
|
93
|
+
function liveParentsResolvable(provider: any, e: any): boolean {
|
|
94
|
+
return e.parents.every((p: string) => {
|
|
95
|
+
const pe = parentEntityFor(provider, e, p)
|
|
96
|
+
return null != pe && pe.cmds.includes('list')
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
// A DECLARED identifier derived from an entity name.
|
|
102
|
+
//
|
|
103
|
+
// apidef canonizes an entity name to `[A-Za-z_0-9]` — `canonize` strips
|
|
104
|
+
// everything else, so hyphens and dots never reach the model and `a-b` and
|
|
105
|
+
// `a_b` arrive as the same `a_b`. The one shape that survives and is NOT a
|
|
106
|
+
// legal identifier is a LEADING DIGIT, which real resources produce:
|
|
107
|
+
// `3ds-sessions` canonizes to `3ds_session`, `2fa-tokens` to `2fa_token`.
|
|
108
|
+
//
|
|
109
|
+
// A DECLARATION cannot be bracket-quoted the way a property access can — the
|
|
110
|
+
// same constraint `guardName` in Main documents — so it is prefixed instead.
|
|
111
|
+
// Only a leading digit is touched, so every ordinary entity keeps the name it
|
|
112
|
+
// has always generated.
|
|
113
|
+
function entVar(name: string, suffix = ''): string {
|
|
114
|
+
return /^[0-9]/.test(name) ? `e_${name}${suffix}` : `${name}${suffix}`
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
// The lines that fetch a live parent id per parent key, plus the guard that
|
|
119
|
+
// skips when the server has no parent record to attach to. Empty for a
|
|
120
|
+
// top-level entity.
|
|
121
|
+
function liveParentSetup(provider: any, e: any, ind: string): string {
|
|
122
|
+
if (0 === e.parents.length) {
|
|
123
|
+
return ''
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return e.parents.map((p: string) => {
|
|
127
|
+
const pe = parentEntityFor(provider, e, p)
|
|
128
|
+
const pv = entVar(pe.name, 'Records')
|
|
129
|
+
|
|
130
|
+
return `${ind} // ${e.name} records hang off ${pe.name} records, so the ${p} has to
|
|
131
|
+
${ind} // come FROM THE SERVER. This database is not ours to seed.
|
|
132
|
+
${ind} const ${pv} = await seneca
|
|
133
|
+
${ind} .entity('provider/${provider.lower}/${pe.name}')
|
|
134
|
+
${ind} .list\$()
|
|
135
|
+
|
|
136
|
+
${ind} if (0 === ${pv}.length) return t.skip('no ${pe.name} to attach a ${e.name} to')
|
|
137
|
+
|
|
138
|
+
${ind} const ${p} = ${pv}[0].${pe.idf || 'id'}
|
|
139
|
+
|
|
140
|
+
`
|
|
141
|
+
}).join('')
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
// A field a round-trip test can CHANGE and then assert on: the first string
|
|
146
|
+
// field that is neither the id nor a parent path param. Without one there is
|
|
147
|
+
// nothing an update could alter that an assertion could see, so the update leg
|
|
148
|
+
// is dropped rather than asserted vacuously.
|
|
149
|
+
function mutableField(e: any): string {
|
|
150
|
+
const f = (e.fields || []).find((f: any) =>
|
|
151
|
+
f.name !== e.idf && 'id' !== f.name &&
|
|
152
|
+
!e.parents.includes(f.name) && 'string' === f.kind)
|
|
153
|
+
|
|
154
|
+
return f ? f.name : ''
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
// A create -> load -> update -> remove round-trip for one entity.
|
|
159
|
+
//
|
|
160
|
+
// Emitted for any entity declaring BOTH save and remove, in both modes: once
|
|
161
|
+
// offline against the SDK's mock transport, once live behind the server probe.
|
|
162
|
+
// The write path is where a provider actually breaks — a save that forgets a
|
|
163
|
+
// parent key, an update that creates a second record instead of amending the
|
|
164
|
+
// first — and it was covered by nothing until this existed. The hand-written
|
|
165
|
+
// provider this target was modelled on had exactly these tests, live; dropping
|
|
166
|
+
// them on the first regeneration left every cmd.save and cmd.remove action in
|
|
167
|
+
// the generated plugin unexecuted by its own suite.
|
|
168
|
+
//
|
|
169
|
+
// The created id is never asserted to a VALUE: both the mock and a real API
|
|
170
|
+
// assign it themselves and ignore any the SDK sends.
|
|
171
|
+
function crudTest(provider: any, e: any, mode: 'offline' | 'live'): string {
|
|
172
|
+
const live = 'live' === mode
|
|
173
|
+
const pairs = parentPairs(e, live)
|
|
174
|
+
// Seneca's key, not the API's: this test drives seneca.entity(...), whose
|
|
175
|
+
// query and entity always spell the id `id`. The provider translates to
|
|
176
|
+
// whatever the API calls it.
|
|
177
|
+
const idf = 'id'
|
|
178
|
+
const mut = mutableField(e)
|
|
179
|
+
|
|
180
|
+
const ind = live ? ' ' : ' '
|
|
181
|
+
const mk = live ? 'makeSeneca(liveOpts())' : 'makeSeneca()'
|
|
182
|
+
const setup = live ? liveParentSetup(provider, e, ind) : ''
|
|
183
|
+
|
|
184
|
+
const made = 0 < e.fields.filter((f: any) =>
|
|
185
|
+
f.name !== idf && 'id' !== f.name && !e.parents.includes(f.name)).length ?
|
|
186
|
+
seedLiteral(e, 'crud') : ''
|
|
187
|
+
|
|
188
|
+
return `${ind}it('${e.name}-crud', async (${live ? 't' : ''}) => {
|
|
189
|
+
${live ? `${ind} if (!live) return t.skip(noServer())\n` : ''}${ind} const seneca = await ${mk}
|
|
190
|
+
${ind} const ent = seneca.entity('provider/${provider.lower}/${e.name}')
|
|
191
|
+
|
|
192
|
+
${setup}${ind} // Seneca's convention: an entity WITHOUT an id is a create. The API
|
|
193
|
+
${ind} // assigns the id itself, so the saved record comes back with one it chose.
|
|
194
|
+
${ind} const made = await ent.make$({ ${pairs}${made} }).save$()
|
|
195
|
+
|
|
196
|
+
${ind} assert.ok(null != made.${idf})
|
|
197
|
+
${ind} assert.equal(
|
|
198
|
+
${ind} made.canon\$({ string: true }),
|
|
199
|
+
${ind} 'provider/${provider.lower}/${e.name}',
|
|
200
|
+
${ind} )
|
|
201
|
+
|
|
202
|
+
${ind} const id = made.${idf}
|
|
203
|
+
|
|
204
|
+
${ind} try {
|
|
205
|
+
${ind} const loaded = await ent.load\$({ ${pairs}${idf}: id })
|
|
206
|
+
${ind} assert.equal(loaded.${idf}, id)
|
|
207
|
+
${
|
|
208
|
+
'' === mut ? '' :
|
|
209
|
+
`
|
|
210
|
+
${ind} // An entity CARRYING an id is an update, not a second create.
|
|
211
|
+
${ind} loaded.${mut} = 'crud-${mut}-2'
|
|
212
|
+
${ind} const updated = await loaded.save\$()
|
|
213
|
+
|
|
214
|
+
${ind} assert.equal(updated.${idf}, id)
|
|
215
|
+
${ind} assert.equal(updated.${mut}, 'crud-${mut}-2')
|
|
216
|
+
|
|
217
|
+
${ind} const reloaded = await ent.load\$({ ${pairs}${idf}: id })
|
|
218
|
+
${ind} assert.equal(reloaded.${mut}, 'crud-${mut}-2')
|
|
219
|
+
`}${ind} }
|
|
220
|
+
${ind} finally {
|
|
221
|
+
${ind} // Always clean up. The mock and the server both hold data for the
|
|
222
|
+
${ind} // process lifetime, so a leaked record changes what later tests see.
|
|
223
|
+
${ind} await ent.remove\$({ ${pairs}${idf}: id })
|
|
224
|
+
${ind} }
|
|
225
|
+
|
|
226
|
+
${ind} // remove is real: the record is gone, and reading it is an ordinary
|
|
227
|
+
${ind} // not-found rather than an error.
|
|
228
|
+
${ind} assert.equal(await ent.load\$({ ${pairs}${idf}: id }), null)
|
|
229
|
+
${ind}})
|
|
230
|
+
|
|
231
|
+
`
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
// A source literal for one field, by kind.
|
|
236
|
+
//
|
|
237
|
+
// `$ARRAY` and `$OBJECT` are in the model's sentinel vocabulary and used to
|
|
238
|
+
// fall through to the string branch, so a list field came out as
|
|
239
|
+
// `tags: 'quick-tags'` — a type-incorrect body that a validating server
|
|
240
|
+
// rejects, and a fixture that quietly stopped exercising non-scalar payloads.
|
|
241
|
+
function fieldLiteral(f: any, tag: string): string {
|
|
242
|
+
switch (f.kind) {
|
|
243
|
+
case 'number': return '12345'
|
|
244
|
+
case 'boolean': return 'true'
|
|
245
|
+
case 'array': return '[]'
|
|
246
|
+
case 'object': return '{}'
|
|
247
|
+
default: return `'${tag}-${f.name}'`
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
// `name: 'value'` pairs for an entity's own (non-id, non-parent) required
|
|
253
|
+
// fields, tagged with `tag` so a test record is recognisable in a store it
|
|
254
|
+
// shares with the seed.
|
|
255
|
+
function seedLiteral(e: any, tag: string): string {
|
|
256
|
+
return (e.fields || [])
|
|
257
|
+
.filter((f: any) =>
|
|
258
|
+
f.name !== e.idf && 'id' !== f.name && !e.parents.includes(f.name))
|
|
259
|
+
.map((f: any) => `${jsKey(f.name)}: ${fieldLiteral(f, tag)}`)
|
|
260
|
+
.join(', ')
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
// A plausible seed record for an entity: its required fields, given values
|
|
265
|
+
// that read as data rather than as `string`.
|
|
266
|
+
function seedRecord(e: any, idx: number): Record<string, any> {
|
|
267
|
+
const out: Record<string, any> = {}
|
|
268
|
+
|
|
269
|
+
for (const f of e.fields) {
|
|
270
|
+
if ('id' === f.name || f.name === e.idf) {
|
|
271
|
+
out[f.name] = `${e.name}${idx}`
|
|
272
|
+
}
|
|
273
|
+
else if (e.parents.includes(f.name)) {
|
|
274
|
+
// A nested entity's parent id must match a record the parent seeds, or
|
|
275
|
+
// the offline store answers nothing and every nested test reads as a
|
|
276
|
+
// false pass. Reuses parentSeed's fallback rather than f.parentEntity
|
|
277
|
+
// directly: when no entity in the model shares this key's name (the
|
|
278
|
+
// common case for a scoping param like `user_id` with no `user`
|
|
279
|
+
// entity, or a same-named response field that means something else
|
|
280
|
+
// entirely, like GitHub's `owner`), f.parentEntity is '' and seeding
|
|
281
|
+
// '0' desynced the record from every query built against the SAME
|
|
282
|
+
// key via parentSeed (parentPairs, crudTest, ...) — 0 results, or a
|
|
283
|
+
// seeded field asserted against the wrong literal.
|
|
284
|
+
out[f.name] = parentSeed(e, f.name)
|
|
285
|
+
}
|
|
286
|
+
else if ('number' === f.kind) {
|
|
287
|
+
out[f.name] = 100 * (idx + 1)
|
|
288
|
+
}
|
|
289
|
+
else if ('boolean' === f.kind) {
|
|
290
|
+
out[f.name] = false
|
|
291
|
+
}
|
|
292
|
+
else if ('array' === f.kind) {
|
|
293
|
+
out[f.name] = []
|
|
294
|
+
}
|
|
295
|
+
else if ('object' === f.kind) {
|
|
296
|
+
out[f.name] = {}
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
out[f.name] = `${f.name}${idx}`
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return out
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
const Tests = cmp(function Tests(props: any) {
|
|
308
|
+
const { provider } = props
|
|
309
|
+
|
|
310
|
+
// The entity the suite exercises hardest: prefer one with no parent keys
|
|
311
|
+
// (nothing to arrange) and the most cmds.
|
|
312
|
+
const subject = [...provider.entities]
|
|
313
|
+
.sort((a: any, b: any) =>
|
|
314
|
+
(a.parents.length - b.parents.length) || (b.cmds.length - a.cmds.length))[0]
|
|
315
|
+
|
|
316
|
+
const nested = provider.entities.filter((e: any) => 0 < e.parents.length)
|
|
317
|
+
|
|
318
|
+
Folder({ name: 'test' }, () => {
|
|
319
|
+
|
|
320
|
+
// The seed the offline mock transport is loaded with. Generated from the
|
|
321
|
+
// model so it matches the shape the SDK will actually return.
|
|
322
|
+
File({ name: 'seed.js' }, () => {
|
|
323
|
+
Content(`/* Generated by @voxgig/sdkgen. Do not edit. */
|
|
324
|
+
'use strict'
|
|
325
|
+
|
|
326
|
+
// Seed data for the SDK's offline mock transport, so the entity tests
|
|
327
|
+
// exercise real code paths without a server.
|
|
328
|
+
const SEED = {
|
|
329
|
+
entity: {
|
|
330
|
+
`)
|
|
331
|
+
each(provider.entities, (e: any) => {
|
|
332
|
+
Content(` ${jsKey(e.name)}: {
|
|
333
|
+
`)
|
|
334
|
+
each([0, 1], (i: any) => {
|
|
335
|
+
const idx = Number(i.val$ ?? i)
|
|
336
|
+
const rec = seedRecord(e, idx)
|
|
337
|
+
Content(` ${jsKey(e.name + idx)}: ${JSON.stringify(rec)},
|
|
338
|
+
`)
|
|
339
|
+
})
|
|
340
|
+
Content(` },
|
|
341
|
+
`)
|
|
342
|
+
})
|
|
343
|
+
Content(` },
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
module.exports = { SEED }
|
|
347
|
+
`)
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
// The message-level spec seneca-msg-test drives. TypeScript, compiled to
|
|
352
|
+
// dist-test by test/tsconfig.json — which is also why it must exist: the
|
|
353
|
+
// shipped tsconfig has `include: ["**/*.ts"]` and tsc fails outright on a
|
|
354
|
+
// config that matches no input.
|
|
355
|
+
File({ name: 'basic.messages.ts' }, () => {
|
|
356
|
+
Content(`/* Generated by @voxgig/sdkgen. Do not edit. */
|
|
357
|
+
|
|
358
|
+
const Pkg = require('../package.json')
|
|
359
|
+
|
|
360
|
+
const messages = {
|
|
361
|
+
print: false,
|
|
362
|
+
pattern: 'sys:provider,provider:${provider.lower}',
|
|
363
|
+
allow: { missing: true },
|
|
364
|
+
|
|
365
|
+
calls: [
|
|
366
|
+
{
|
|
367
|
+
pattern: 'get:info',
|
|
368
|
+
out: {
|
|
369
|
+
ok: true,
|
|
370
|
+
name: '${provider.lower}',
|
|
371
|
+
version: Pkg.version,
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
],
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export default messages
|
|
378
|
+
|
|
379
|
+
if ('undefined' !== typeof module) {
|
|
380
|
+
module.exports = messages
|
|
381
|
+
}
|
|
382
|
+
`)
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
File({ name: `${provider.fileBase}.test.js` }, () => {
|
|
387
|
+
Content(`/* Generated by @voxgig/sdkgen. Do not edit. */
|
|
388
|
+
'use strict'
|
|
389
|
+
|
|
390
|
+
const { describe, it, before } = require('node:test')
|
|
391
|
+
const assert = require('node:assert')
|
|
392
|
+
|
|
393
|
+
const Seneca = require('seneca')
|
|
394
|
+
|
|
395
|
+
const ${provider.pluginName} = require('../dist/${provider.fileBase}')
|
|
396
|
+
const ${provider.pluginName}Doc = require('../dist/${provider.pluginName}-doc')
|
|
397
|
+
|
|
398
|
+
const SenecaMsgTest = require('seneca-msg-test')
|
|
399
|
+
const { Maintain } = require('@seneca/maintain')
|
|
400
|
+
|
|
401
|
+
const { SEED } = require('./seed')
|
|
402
|
+
|
|
403
|
+
const BasicMessages = require('../dist-test/basic.messages')
|
|
404
|
+
${'' === provider.liveBase ? '' : `
|
|
405
|
+
// The live tests run against the companion test server in the SDK repo
|
|
406
|
+
// (\`app/\`), which serves this by default. Start it with:
|
|
407
|
+
// cd ${provider.sdkrel}/app && npm start
|
|
408
|
+
const LIVE_BASE = process.env.${provider.ENV}_TEST_BASE || '${provider.liveBase}'
|
|
409
|
+
`}
|
|
410
|
+
|
|
411
|
+
describe('${provider.fileBase}', () => {
|
|
412
|
+
|
|
413
|
+
it('happy', async () => {
|
|
414
|
+
assert.notEqual(${provider.pluginName}, undefined)
|
|
415
|
+
assert.notEqual(${provider.pluginName}Doc, undefined)
|
|
416
|
+
|
|
417
|
+
const seneca = await makeSeneca()
|
|
418
|
+
|
|
419
|
+
assert.partialDeepStrictEqual(
|
|
420
|
+
await seneca.post('sys:provider,provider:${provider.lower},get:info'),
|
|
421
|
+
{
|
|
422
|
+
ok: true,
|
|
423
|
+
name: '${provider.lower}',
|
|
424
|
+
},
|
|
425
|
+
)
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
it('messages', async () => {
|
|
430
|
+
const seneca = await makeSeneca()
|
|
431
|
+
await SenecaMsgTest(seneca, BasicMessages)()
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
it('sdk-export', async () => {
|
|
436
|
+
const seneca = await makeSeneca()
|
|
437
|
+
const sdk = seneca.export('${provider.pluginName}/sdk')()
|
|
438
|
+
|
|
439
|
+
`)
|
|
440
|
+
each(provider.entities, (e: any) => {
|
|
441
|
+
Content(` assert.equal(typeof sdk.${e.acc}, 'function')
|
|
442
|
+
`)
|
|
443
|
+
})
|
|
444
|
+
Content(` })
|
|
445
|
+
|
|
446
|
+
`)
|
|
447
|
+
|
|
448
|
+
// Every flat entity (no parent keys), not just one "subject" — a
|
|
449
|
+
// provider with two or more flat siblings used to leave every one
|
|
450
|
+
// but the busiest untested beyond the accessor check above. A bare
|
|
451
|
+
// `list$()`/`load$(id)` call has no way to carry a parent key, so
|
|
452
|
+
// entities that need one are covered by the `nested` block below
|
|
453
|
+
// instead, with their keys filled in.
|
|
454
|
+
const flat = provider.entities.filter((e: any) => 0 === e.parents.length)
|
|
455
|
+
|
|
456
|
+
each(flat, (e: any) => {
|
|
457
|
+
if (e.cmds.includes('list')) {
|
|
458
|
+
Content(`
|
|
459
|
+
it('${e.name}-list', async () => {
|
|
460
|
+
const seneca = await makeSeneca()
|
|
461
|
+
const list = await seneca.entity('provider/${provider.lower}/${e.name}').list$()
|
|
462
|
+
|
|
463
|
+
assert.equal(list.length, 2)
|
|
464
|
+
|
|
465
|
+
// Entities must come back as Seneca entities under this plugin's canon.
|
|
466
|
+
// The SDK tags its own results with its entity marker, which must not
|
|
467
|
+
// survive into the Seneca entity.
|
|
468
|
+
assert.equal(
|
|
469
|
+
list[0].canon$({ string: true }),
|
|
470
|
+
'provider/${provider.lower}/${e.name}',
|
|
471
|
+
)
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
`)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (e.cmds.includes('load')) {
|
|
478
|
+
Content(`
|
|
479
|
+
it('${e.name}-load', async () => {
|
|
480
|
+
const seneca = await makeSeneca()
|
|
481
|
+
const found = await seneca
|
|
482
|
+
.entity('provider/${provider.lower}/${e.name}')
|
|
483
|
+
.load$('${e.name}0')
|
|
484
|
+
|
|
485
|
+
assert.equal(found.${e.idf || 'id'}, '${e.name}0')
|
|
486
|
+
assert.equal(
|
|
487
|
+
found.canon$({ string: true }),
|
|
488
|
+
'provider/${provider.lower}/${e.name}',
|
|
489
|
+
)
|
|
490
|
+
})
|
|
491
|
+
|
|
492
|
+
`)
|
|
493
|
+
// Paramless read (e.g. GET /usage): every id "misses" the same
|
|
494
|
+
// way a hit does -- the mock has nothing to filter by -- so a
|
|
495
|
+
// load-missing test would just assert the happy path again.
|
|
496
|
+
if (loadHasKey(e.ent)) {
|
|
497
|
+
Content(`
|
|
498
|
+
// A 404 from a single-item read is an ordinary "not found" answer, not a
|
|
499
|
+
// failure: the provider turns it into null rather than letting the SDK
|
|
500
|
+
// throw.
|
|
501
|
+
it('${e.name}-load-missing', async () => {
|
|
502
|
+
const seneca = await makeSeneca()
|
|
503
|
+
const missing = await seneca
|
|
504
|
+
.entity('provider/${provider.lower}/${e.name}')
|
|
505
|
+
.load$('nosuch${e.name}')
|
|
506
|
+
|
|
507
|
+
assert.equal(missing, null)
|
|
508
|
+
})
|
|
509
|
+
|
|
510
|
+
`)
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
})
|
|
514
|
+
|
|
515
|
+
// A nested entity cannot build its path without the parent id. That is
|
|
516
|
+
// the mistake this target exists to make impossible, so pin it.
|
|
517
|
+
each(nested, (e: any) => {
|
|
518
|
+
// EVERY parent key, not just the first. An entity nested two levels
|
|
519
|
+
// deep is guarded on both, so a test supplying only the alphabetically
|
|
520
|
+
// first tripped the second guard and failed on the code it was meant
|
|
521
|
+
// to be exercising.
|
|
522
|
+
const key = e.parents[0]
|
|
523
|
+
const pairs = e.parents
|
|
524
|
+
.map((k: string) => `${k}: '${parentSeed(e, k)}'`).join(', ')
|
|
525
|
+
|
|
526
|
+
// The guard is PER OP (Main's opParents), not a blanket property of
|
|
527
|
+
// the entity, so the op this test calls has to be one that actually
|
|
528
|
+
// requires `key` — hardcoding `list` assumed every nested entity's
|
|
529
|
+
// list is parent-scoped, which fails for e.g. an entity guarded on
|
|
530
|
+
// load/update/remove but whose list is unscoped (GitHub's `repo`:
|
|
531
|
+
// owner guards load, not list).
|
|
532
|
+
const guardOp = ['list', 'load', 'update', 'remove']
|
|
533
|
+
.find((op: string) => (e.opParents[op] || []).includes(key))
|
|
534
|
+
|
|
535
|
+
if (null != guardOp) {
|
|
536
|
+
const call = 'list' === guardOp ?
|
|
537
|
+
`${guardOp}$({})` : `${guardOp}$({ id: '${e.name}0' })`
|
|
538
|
+
|
|
539
|
+
Content(`
|
|
540
|
+
it('${e.name}-needs-${key}', async () => {
|
|
541
|
+
const seneca = await makeSeneca()
|
|
542
|
+
|
|
543
|
+
await assert.rejects(
|
|
544
|
+
() => seneca.entity('provider/${provider.lower}/${e.name}').${call},
|
|
545
|
+
/${key} is required/,
|
|
546
|
+
)
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
`)
|
|
550
|
+
}
|
|
551
|
+
if (e.cmds.includes('list')) {
|
|
552
|
+
// Assert on the SEEDED RECORDS, not merely that an array came back.
|
|
553
|
+
// `Array.isArray` is true of the empty array, so the nested-list
|
|
554
|
+
// test passed while proving nothing: the seed puts both of this
|
|
555
|
+
// entity's records under the same parent, so both must come back,
|
|
556
|
+
// under this plugin's canon, still carrying the parent key that
|
|
557
|
+
// addressed them.
|
|
558
|
+
Content(`
|
|
559
|
+
it('${e.name}-list', async () => {
|
|
560
|
+
const seneca = await makeSeneca()
|
|
561
|
+
const list = await seneca
|
|
562
|
+
.entity('provider/${provider.lower}/${e.name}')
|
|
563
|
+
.list$({ ${pairs} })
|
|
564
|
+
|
|
565
|
+
assert.equal(list.length, 2)
|
|
566
|
+
assert.equal(
|
|
567
|
+
list[0].canon$({ string: true }),
|
|
568
|
+
'provider/${provider.lower}/${e.name}',
|
|
569
|
+
)
|
|
570
|
+
assert.equal(list[0].${key}, '${parentSeed(e, key)}')
|
|
571
|
+
})
|
|
572
|
+
|
|
573
|
+
`)
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Reading ONE nested record is the path that has to thread both the
|
|
577
|
+
// parent id and the entity id through to the SDK, so cover it
|
|
578
|
+
// separately from list.
|
|
579
|
+
if (e.cmds.includes('load')) {
|
|
580
|
+
Content(`
|
|
581
|
+
it('${e.name}-load', async () => {
|
|
582
|
+
const seneca = await makeSeneca()
|
|
583
|
+
const found = await seneca
|
|
584
|
+
.entity('provider/${provider.lower}/${e.name}')
|
|
585
|
+
.load$({ ${pairs}, id: '${e.name}0' })
|
|
586
|
+
|
|
587
|
+
assert.equal(found.id, '${e.name}0')
|
|
588
|
+
assert.equal(
|
|
589
|
+
found.canon$({ string: true }),
|
|
590
|
+
'provider/${provider.lower}/${e.name}',
|
|
591
|
+
)
|
|
592
|
+
})
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
it('${e.name}-load-missing', async () => {
|
|
596
|
+
const seneca = await makeSeneca()
|
|
597
|
+
const missing = await seneca
|
|
598
|
+
.entity('provider/${provider.lower}/${e.name}')
|
|
599
|
+
.load$({ ${pairs}, id: 'nosuch${e.name}' })
|
|
600
|
+
|
|
601
|
+
assert.equal(missing, null)
|
|
602
|
+
})
|
|
603
|
+
|
|
604
|
+
`)
|
|
605
|
+
}
|
|
606
|
+
})
|
|
607
|
+
|
|
608
|
+
// The WRITE path, offline. Reads were covered and writes were not, so
|
|
609
|
+
// every generated `cmd.save` and `cmd.remove` action shipped without its
|
|
610
|
+
// own suite ever running it — including the parent-key guard on a nested
|
|
611
|
+
// save, which is the one this target exists to get right. The mock
|
|
612
|
+
// transport implements create/update/remove, so this needs no server.
|
|
613
|
+
each(provider.entities, (e: any) => {
|
|
614
|
+
if (e.cmds.includes('save') && e.cmds.includes('remove')) {
|
|
615
|
+
Content(`
|
|
616
|
+
` + crudTest(provider, e, 'offline'))
|
|
617
|
+
}
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
// ACTIONS — the `action$` directive.
|
|
622
|
+
//
|
|
623
|
+
// The test that matters most is the NEGATIVE one. A name this entity
|
|
624
|
+
// does not have must throw, because the alternative is that the plugin
|
|
625
|
+
// ignores the key and performs an ordinary save: a call that succeeds,
|
|
626
|
+
// reports success, and did something else. That is exactly how GitHub's
|
|
627
|
+
// `merge` reached its provider as an "update" — the endpoint existed,
|
|
628
|
+
// the plugin had no way to name it, and nothing said so.
|
|
629
|
+
//
|
|
630
|
+
// Generated for EVERY entity, whether it has actions or not: an entity
|
|
631
|
+
// with none is the case most likely to be typed at by mistake, and its
|
|
632
|
+
// error is the one that names the empty set.
|
|
633
|
+
each(provider.entities, (e: any) => {
|
|
634
|
+
const pairs = parentPairs(e, false)
|
|
635
|
+
const acts = e.actionList.filter((a: any) => 'save' === a.cmd)
|
|
636
|
+
|
|
637
|
+
if (e.cmds.includes('save')) {
|
|
638
|
+
Content(`
|
|
639
|
+
it('${e.name}-action-unknown-save', async () => {
|
|
640
|
+
const seneca = await makeSeneca()
|
|
641
|
+
|
|
642
|
+
await assert.rejects(
|
|
643
|
+
() => seneca.entity('provider/${provider.lower}/${e.name}')
|
|
644
|
+
.make$({ ${pairs}id: '${e.name}0' })
|
|
645
|
+
.directive$({ action$: 'no_such_action' })
|
|
646
|
+
.save$(),
|
|
647
|
+
/action\\$ "no_such_action" is not an action/,
|
|
648
|
+
)
|
|
649
|
+
})
|
|
650
|
+
|
|
651
|
+
`)
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (e.cmds.includes('list')) {
|
|
655
|
+
Content(`
|
|
656
|
+
it('${e.name}-action-unknown-list', async () => {
|
|
657
|
+
const seneca = await makeSeneca()
|
|
658
|
+
|
|
659
|
+
await assert.rejects(
|
|
660
|
+
() => seneca.entity('provider/${provider.lower}/${e.name}')
|
|
661
|
+
.list$({ ${pairs}action$: 'no_such_action' }),
|
|
662
|
+
/action\\$ "no_such_action" is not an action/,
|
|
663
|
+
)
|
|
664
|
+
})
|
|
665
|
+
|
|
666
|
+
`)
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// THE SILENT-DROP PIN. A save with no `action$` must still take the
|
|
670
|
+
// canonical route: the whole mechanism is worthless if adding it
|
|
671
|
+
// changed what an ordinary call does, and this is the assertion that
|
|
672
|
+
// would fail if the action branch ever ran unconditionally.
|
|
673
|
+
//
|
|
674
|
+
// GATED ON THE ENTITY BEING ABLE TO PERFORM ONE, which is three
|
|
675
|
+
// separate facts and was none of them. The test loads a record, edits
|
|
676
|
+
// it and saves it back, so it needs a `load` cmd to fetch with, a
|
|
677
|
+
// canonical `update` route to save to — an entity whose only update
|
|
678
|
+
// point is the action has no plain save at all — and a mutable field
|
|
679
|
+
// to change. Emitted without those it ships a red suite to a package
|
|
680
|
+
// whose action works perfectly, which is the worst kind of generated
|
|
681
|
+
// test: it fails for a reason that is not about the code it names.
|
|
682
|
+
//
|
|
683
|
+
// The ACTION tests below are not gated on any of this. They are what
|
|
684
|
+
// this entity does have.
|
|
685
|
+
const canPlainSave = e.cmds.includes('load') &&
|
|
686
|
+
e.canonicalOps.includes('update')
|
|
687
|
+
|
|
688
|
+
if (0 < acts.length && e.cmds.includes('save')) {
|
|
689
|
+
const mut = canPlainSave ? mutableField(e) : ''
|
|
690
|
+
if ('' !== mut) {
|
|
691
|
+
Content(`
|
|
692
|
+
// No action$ named, so this is the plain update — the action route must
|
|
693
|
+
// not run on a call that did not ask for it.
|
|
694
|
+
it('${e.name}-save-without-action', async () => {
|
|
695
|
+
const seneca = await makeSeneca()
|
|
696
|
+
const ent = seneca.entity('provider/${provider.lower}/${e.name}')
|
|
697
|
+
|
|
698
|
+
const loaded = await ent.load$({ ${pairs}id: '${e.name}0' })
|
|
699
|
+
loaded.${mut} = 'plain-${mut}'
|
|
700
|
+
const saved = await loaded.save$()
|
|
701
|
+
|
|
702
|
+
assert.equal(saved.${mut}, 'plain-${mut}')
|
|
703
|
+
assert.equal(
|
|
704
|
+
saved.canon$({ string: true }),
|
|
705
|
+
'provider/${provider.lower}/${e.name}',
|
|
706
|
+
)
|
|
707
|
+
})
|
|
708
|
+
|
|
709
|
+
`)
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// And the POSITIVE case: a name the entity DOES have is accepted
|
|
713
|
+
// and dispatched. `directive$` rather than `make$({ action$ })`
|
|
714
|
+
// because make$ drops an unknown trailing-`$` key before any store
|
|
715
|
+
// sees it — see the README's Actions section.
|
|
716
|
+
//
|
|
717
|
+
// WHAT THIS DOES NOT ASSERT, and why. The offline mock answers by
|
|
718
|
+
// matching a seeded record against the parameters of the point the
|
|
719
|
+
// SDK chose, and the seed is built for the CANONICAL route — an
|
|
720
|
+
// action route with parameters of its own has nothing seeded to
|
|
721
|
+
// match, so the mock's honest answer is a 404. Asserting a returned
|
|
722
|
+
// record here would mean generating a test that fails for every API
|
|
723
|
+
// whose actions are not shaped like its CRUD.
|
|
724
|
+
//
|
|
725
|
+
// The provider's own responsibility is to accept the name and route
|
|
726
|
+
// it. That is what is asserted: whatever comes back, it is not this
|
|
727
|
+
// plugin refusing the action. Paired with the unknown-action test
|
|
728
|
+
// above, the two together say the map holds exactly the right names.
|
|
729
|
+
const act = acts[0]
|
|
730
|
+
Content(`
|
|
731
|
+
// \`${act.action}\` is an action of \`${act.op}\`: ${act.path}
|
|
732
|
+
it('${e.name}-action-${act.action}', async () => {
|
|
733
|
+
const seneca = await makeSeneca()
|
|
734
|
+
let err = null
|
|
735
|
+
|
|
736
|
+
try {
|
|
737
|
+
await seneca.entity('provider/${provider.lower}/${e.name}')
|
|
738
|
+
.make$({ ${pairs}id: '${e.name}0' })
|
|
739
|
+
.directive$({ action$: '${act.action}' })
|
|
740
|
+
.save$()
|
|
741
|
+
}
|
|
742
|
+
catch (e) { err = e }
|
|
743
|
+
|
|
744
|
+
if (null != err) {
|
|
745
|
+
assert.ok(!/is not an action/.test(err.message),
|
|
746
|
+
'the action was refused instead of routed: ' + err.message)
|
|
747
|
+
}
|
|
748
|
+
})
|
|
749
|
+
|
|
750
|
+
`)
|
|
751
|
+
}
|
|
752
|
+
})
|
|
753
|
+
|
|
754
|
+
// Live tests, against the companion server in the SDK repo's `app/`.
|
|
755
|
+
// They PROBE first and skip when nothing is listening, so the suite is
|
|
756
|
+
// green on a machine that has never started it — a live suite that
|
|
757
|
+
// fails when the server is absent is one nobody runs.
|
|
758
|
+
if ('' !== provider.liveBase) {
|
|
759
|
+
Content(`
|
|
760
|
+
describe('live', () => {
|
|
761
|
+
let live = false
|
|
762
|
+
|
|
763
|
+
before(async () => {
|
|
764
|
+
live = await serverUp(LIVE_BASE)
|
|
765
|
+
})
|
|
766
|
+
|
|
767
|
+
`)
|
|
768
|
+
if (subject.cmds.includes('list')) {
|
|
769
|
+
Content(` it('${subject.name}-list', async (t) => {
|
|
770
|
+
if (!live) return t.skip(noServer())
|
|
771
|
+
const seneca = await makeSeneca(liveOpts())
|
|
772
|
+
|
|
773
|
+
const list = await seneca.entity('provider/${provider.lower}/${subject.name}').list$()
|
|
774
|
+
|
|
775
|
+
assert.ok(Array.isArray(list))
|
|
776
|
+
if (0 < list.length) {
|
|
777
|
+
assert.equal(
|
|
778
|
+
list[0].canon$({ string: true }),
|
|
779
|
+
'provider/${provider.lower}/${subject.name}',
|
|
780
|
+
)
|
|
781
|
+
}
|
|
782
|
+
})
|
|
783
|
+
|
|
784
|
+
`)
|
|
785
|
+
}
|
|
786
|
+
if (subject.cmds.includes('load')) {
|
|
787
|
+
Content(` // A read of something that is not there is \`null\`, live as well as
|
|
788
|
+
// offline: the provider's 404 handling is the same code path either way.
|
|
789
|
+
it('${subject.name}-load-missing', async (t) => {
|
|
790
|
+
if (!live) return t.skip(noServer())
|
|
791
|
+
const seneca = await makeSeneca(liveOpts())
|
|
792
|
+
|
|
793
|
+
assert.equal(
|
|
794
|
+
await seneca
|
|
795
|
+
.entity('provider/${provider.lower}/${subject.name}')
|
|
796
|
+
.load$('nosuch${subject.name}'),
|
|
797
|
+
null,
|
|
798
|
+
)
|
|
799
|
+
})
|
|
800
|
+
|
|
801
|
+
`)
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// The write path against a REAL server. The mock answers the shape the
|
|
805
|
+
// SDK expects by construction; only a live run proves the request the
|
|
806
|
+
// provider builds is one the API actually accepts — which for a nested
|
|
807
|
+
// entity means the parent id reached the URL rather than the body.
|
|
808
|
+
//
|
|
809
|
+
// Emitted only when a live parent id is OBTAINABLE (see
|
|
810
|
+
// liveParentsResolvable): against a real server the parent has to be
|
|
811
|
+
// looked up, and an entity whose parent cannot be listed offers no
|
|
812
|
+
// honest way to get one.
|
|
813
|
+
each(provider.entities, (e: any) => {
|
|
814
|
+
if (e.cmds.includes('save') && e.cmds.includes('remove') &&
|
|
815
|
+
liveParentsResolvable(provider, e)) {
|
|
816
|
+
Content(crudTest(provider, e, 'live'))
|
|
817
|
+
}
|
|
818
|
+
})
|
|
819
|
+
|
|
820
|
+
Content(` })
|
|
821
|
+
|
|
822
|
+
`)
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// Repository hygiene, from the @seneca/maintain dependency this package
|
|
826
|
+
// declares. Two of its checks report a fault that is not there, because
|
|
827
|
+
// of WHERE they run rather than what they find, so each is excluded
|
|
828
|
+
// only in the environments that break it.
|
|
829
|
+
Content(`
|
|
830
|
+
it('maintain', async () => {
|
|
831
|
+
const exclude = []
|
|
832
|
+
|
|
833
|
+
// check_default proves the default branch is main by looking for
|
|
834
|
+
// [branch "main"] in .git/config. Only a branch checkout records that
|
|
835
|
+
// section: a pull_request build checks out the merge ref, and the
|
|
836
|
+
// publish build checks out a tag as a detached HEAD. Neither says
|
|
837
|
+
// anything about what the default branch is, so skip rather than fail.
|
|
838
|
+
if ('pull_request' === process.env.GITHUB_EVENT_NAME ||
|
|
839
|
+
'tag' === process.env.GITHUB_REF_TYPE) {
|
|
840
|
+
exclude.push('check_default')
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// url_pkgjson locates package.json by comparing process.cwd() + '/package.json'
|
|
844
|
+
// against a path found with Filehound. On Windows those are the same file
|
|
845
|
+
// spelt with different separators, so the url is never read.
|
|
846
|
+
if ('win32' === process.platform) {
|
|
847
|
+
exclude.push('url_pkgjson')
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
await Maintain({ exclude })
|
|
851
|
+
})
|
|
852
|
+
|
|
853
|
+
`)
|
|
854
|
+
|
|
855
|
+
Content(`})
|
|
856
|
+
|
|
857
|
+
`)
|
|
858
|
+
|
|
859
|
+
if ('' !== provider.liveBase) {
|
|
860
|
+
Content(`
|
|
861
|
+
function noServer() {
|
|
862
|
+
return 'no ${provider.lower} server at ' + LIVE_BASE
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
function liveOpts() {
|
|
867
|
+
return { sdk: { base: LIVE_BASE } }
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
// Probe the companion test server so live tests skip cleanly when it is not
|
|
872
|
+
// running, rather than failing the suite.
|
|
873
|
+
async function serverUp(base) {
|
|
874
|
+
try {
|
|
875
|
+
const res = await fetch(base + '${provider.probePath}', {
|
|
876
|
+
signal: AbortSignal.timeout(2000),
|
|
877
|
+
})
|
|
878
|
+
return res.ok
|
|
879
|
+
}
|
|
880
|
+
catch (e) {
|
|
881
|
+
return false
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
`)
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
Content(`
|
|
889
|
+
// Default to the SDK's offline mock transport, seeded from ./seed.
|
|
890
|
+
async function makeSeneca(pluginopts) {
|
|
891
|
+
pluginopts = pluginopts || { test: true, testopts: SEED }
|
|
892
|
+
|
|
893
|
+
const seneca = Seneca({ legacy: false })
|
|
894
|
+
.test()
|
|
895
|
+
.use('promisify')
|
|
896
|
+
.use('entity')
|
|
897
|
+
.use('env', {
|
|
898
|
+
// Declared so the provider convention is exercised, and defaulted so
|
|
899
|
+
// the suite runs with nothing configured.
|
|
900
|
+
var: {
|
|
901
|
+
$${provider.ENV}_APIKEY: '',
|
|
902
|
+
},
|
|
903
|
+
})
|
|
904
|
+
.use('provider', {
|
|
905
|
+
provider: {
|
|
906
|
+
${provider.lower}: {
|
|
907
|
+
keys: {
|
|
908
|
+
apikey: { value: '$${provider.ENV}_APIKEY' },
|
|
909
|
+
},
|
|
910
|
+
},
|
|
911
|
+
},
|
|
912
|
+
})
|
|
913
|
+
.use(${provider.pluginName}, pluginopts)
|
|
914
|
+
|
|
915
|
+
return seneca.ready()
|
|
916
|
+
}
|
|
917
|
+
`)
|
|
918
|
+
})
|
|
919
|
+
})
|
|
920
|
+
})
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
// --- test/live.js, test/quick.js --------------------------------------------
|
|
924
|
+
//
|
|
925
|
+
// Manual scripts, not part of `npm test`: they need the companion server in
|
|
926
|
+
// the SDK repo's `app/`, which is not published. Generated because the path
|
|
927
|
+
// to that server is knowable — it is the inverse of this target's own
|
|
928
|
+
// `output: path` — so the instruction can be exact rather than "start the
|
|
929
|
+
// server somehow".
|
|
930
|
+
|
|
931
|
+
const Scripts = cmp(function Scripts(props: any) {
|
|
932
|
+
const { provider } = props
|
|
933
|
+
|
|
934
|
+
// Nothing to point at without a declared server.
|
|
935
|
+
if ('' === provider.liveBase) {
|
|
936
|
+
return
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
const subject = [...provider.entities]
|
|
940
|
+
.sort((a: any, b: any) =>
|
|
941
|
+
(a.parents.length - b.parents.length) || (b.cmds.length - a.cmds.length))[0]
|
|
942
|
+
|
|
943
|
+
const senecaSetup = `const Seneca = require('seneca')
|
|
944
|
+
|
|
945
|
+
const BASE = process.env.${provider.ENV}_TEST_BASE || '${provider.liveBase}'
|
|
946
|
+
|
|
947
|
+
async function makeSeneca() {
|
|
948
|
+
return Seneca({ legacy: false })
|
|
949
|
+
.test()
|
|
950
|
+
.use('promisify')
|
|
951
|
+
.use('entity')
|
|
952
|
+
.use('provider', {
|
|
953
|
+
provider: {
|
|
954
|
+
${provider.lower}: {
|
|
955
|
+
keys: {
|
|
956
|
+
apikey: { value: '' },
|
|
957
|
+
},
|
|
958
|
+
},
|
|
959
|
+
},
|
|
960
|
+
})
|
|
961
|
+
.use('..', { sdk: { base: BASE } })
|
|
962
|
+
.ready()
|
|
963
|
+
}
|
|
964
|
+
`
|
|
965
|
+
|
|
966
|
+
Folder({ name: 'test' }, () => {
|
|
967
|
+
|
|
968
|
+
File({ name: 'live.js' }, () => {
|
|
969
|
+
Content(`/* Manual script: read from a running ${provider.api} server.
|
|
970
|
+
*
|
|
971
|
+
* Start the companion test server from the SDK repo first:
|
|
972
|
+
* cd ${provider.sdkrel}/app && npm start
|
|
973
|
+
*
|
|
974
|
+
* Then: node test/live.js
|
|
975
|
+
*/
|
|
976
|
+
|
|
977
|
+
${senecaSetup}
|
|
978
|
+
|
|
979
|
+
run()
|
|
980
|
+
|
|
981
|
+
async function run() {
|
|
982
|
+
const seneca = await makeSeneca()
|
|
983
|
+
|
|
984
|
+
`)
|
|
985
|
+
// A nested entity's list needs its parent's id, so LOOK ONE UP rather
|
|
986
|
+
// than emitting a placeholder: a script that 404s on first run teaches
|
|
987
|
+
// nothing and reads as a broken provider.
|
|
988
|
+
each(provider.entities.filter((e: any) => e.cmds.includes('list')), (e: any) => {
|
|
989
|
+
if (0 === e.parents.length) {
|
|
990
|
+
Content(` console.log('${e.name.toUpperCase()}', await seneca
|
|
991
|
+
.entity('provider/${provider.lower}/${e.name}')
|
|
992
|
+
.list$())
|
|
993
|
+
|
|
994
|
+
`)
|
|
995
|
+
return
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const parent = provider.entities
|
|
999
|
+
.find((p: any) => p.name === e.parentEntity && p.cmds.includes('list'))
|
|
1000
|
+
if (null == parent) {
|
|
1001
|
+
// Nothing to derive the parent id from. Say so in the script
|
|
1002
|
+
// rather than emitting a call that cannot work.
|
|
1003
|
+
Content(` // ${e.name}: needs ${e.parents.join(', ')}; no listable parent to take
|
|
1004
|
+
// one from, so supply it yourself:
|
|
1005
|
+
// await seneca.entity('provider/${provider.lower}/${e.name}')
|
|
1006
|
+
// .list$({ ${e.parents.map((k: string) => `${k}: '...'`).join(', ')} })
|
|
1007
|
+
|
|
1008
|
+
`)
|
|
1009
|
+
return
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
const key = e.parents[0]
|
|
1013
|
+
Content(` const ${parent.name}s = await seneca
|
|
1014
|
+
.entity('provider/${provider.lower}/${parent.name}')
|
|
1015
|
+
.list$()
|
|
1016
|
+
|
|
1017
|
+
if (0 < ${parent.name}s.length) {
|
|
1018
|
+
console.log('${e.name.toUpperCase()}', await seneca
|
|
1019
|
+
.entity('provider/${provider.lower}/${e.name}')
|
|
1020
|
+
.list$({ ${key}: ${parent.name}s[0].${parent.idf || 'id'} }))
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
`)
|
|
1024
|
+
})
|
|
1025
|
+
Content(`}
|
|
1026
|
+
`)
|
|
1027
|
+
})
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
// The write cycle, kept separate: it MUTATES the server, so it is not
|
|
1031
|
+
// something to run by reflex. It cleans up after itself.
|
|
1032
|
+
if (subject.cmds.includes('save') && subject.cmds.includes('remove')) {
|
|
1033
|
+
const idf = subject.idf || 'id'
|
|
1034
|
+
const writable = subject.fields
|
|
1035
|
+
.filter((f: any) => f.name !== idf && f.name !== 'id')
|
|
1036
|
+
.filter((f: any) => !subject.parents.includes(f.name))
|
|
1037
|
+
|
|
1038
|
+
const make = writable
|
|
1039
|
+
.map((f: any) => `${jsKey(f.name)}: ${fieldLiteral(f, 'quick')}`)
|
|
1040
|
+
.join(', ')
|
|
1041
|
+
|
|
1042
|
+
File({ name: 'quick.js' }, () => {
|
|
1043
|
+
Content(`/* Manual script: exercise the full CRUD cycle against a running server.
|
|
1044
|
+
*
|
|
1045
|
+
* Start the companion test server from the SDK repo first:
|
|
1046
|
+
* cd ${provider.sdkrel}/app && npm start
|
|
1047
|
+
*
|
|
1048
|
+
* Then: node test/quick.js
|
|
1049
|
+
*
|
|
1050
|
+
* Creates and then removes a ${subject.name}, so the server is left as found.
|
|
1051
|
+
*/
|
|
1052
|
+
|
|
1053
|
+
${senecaSetup}
|
|
1054
|
+
|
|
1055
|
+
run()
|
|
1056
|
+
|
|
1057
|
+
async function run() {
|
|
1058
|
+
const seneca = await makeSeneca()
|
|
1059
|
+
|
|
1060
|
+
// Create: the API assigns the id, so none is supplied here.
|
|
1061
|
+
let ${subject.name} = await seneca
|
|
1062
|
+
.entity('provider/${provider.lower}/${subject.name}')
|
|
1063
|
+
.make$({ ${make} })
|
|
1064
|
+
.save$()
|
|
1065
|
+
console.log('CREATED', ${subject.name})
|
|
1066
|
+
|
|
1067
|
+
const id = ${subject.name}.${idf}
|
|
1068
|
+
|
|
1069
|
+
try {
|
|
1070
|
+
`)
|
|
1071
|
+
// Change something an assertion could SEE. A container field would be
|
|
1072
|
+
// rewritten to the same empty literal, which demonstrates nothing.
|
|
1073
|
+
const upd = writable.find((f: any) =>
|
|
1074
|
+
'string' === f.kind || 'number' === f.kind) || null
|
|
1075
|
+
|
|
1076
|
+
if (subject.ops.includes('update') && null != upd) {
|
|
1077
|
+
const f = upd
|
|
1078
|
+
const v = 'number' === f.kind ? '4321' : `'quick-${f.name}-2'`
|
|
1079
|
+
Content(` // Update: an entity carrying an id is an update.
|
|
1080
|
+
${jsProp(subject.name, f.name)} = ${v}
|
|
1081
|
+
console.log('UPDATED', await ${subject.name}.save$())
|
|
1082
|
+
|
|
1083
|
+
`)
|
|
1084
|
+
}
|
|
1085
|
+
if (subject.cmds.includes('load')) {
|
|
1086
|
+
Content(` console.log(
|
|
1087
|
+
'LOADED',
|
|
1088
|
+
await seneca.entity('provider/${provider.lower}/${subject.name}').load$(id)
|
|
1089
|
+
)
|
|
1090
|
+
|
|
1091
|
+
`)
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
// The NESTED write, which is the leg worth having a manual script
|
|
1095
|
+
// for: it is the one where the parent id has to reach the URL rather
|
|
1096
|
+
// than the body, and where a provider that forgets it reports an
|
|
1097
|
+
// opaque 404 instead of saying what is missing.
|
|
1098
|
+
//
|
|
1099
|
+
// Only for a child of the record just created — then the parent id is
|
|
1100
|
+
// `id`, already in hand, and removing the child leaves the server
|
|
1101
|
+
// exactly as found. A child of anything else would need its own
|
|
1102
|
+
// lookup, which belongs in the test suite rather than in a script
|
|
1103
|
+
// whose whole point is to be readable.
|
|
1104
|
+
const child = provider.entities.find((e: any) =>
|
|
1105
|
+
1 === e.parents.length &&
|
|
1106
|
+
e.parentEntity === subject.name &&
|
|
1107
|
+
e.cmds.includes('save') && e.cmds.includes('remove'))
|
|
1108
|
+
|
|
1109
|
+
if (null != child) {
|
|
1110
|
+
const ckey = child.parents[0]
|
|
1111
|
+
const cidf = child.idf || 'id'
|
|
1112
|
+
const cmake = (child.fields || [])
|
|
1113
|
+
.filter((f: any) =>
|
|
1114
|
+
f.name !== cidf && 'id' !== f.name && !child.parents.includes(f.name))
|
|
1115
|
+
.map((f: any) => `${jsKey(f.name)}: ${fieldLiteral(f, 'quick')}`)
|
|
1116
|
+
.join(', ')
|
|
1117
|
+
|
|
1118
|
+
Content(` // ${child.name} records hang off ${subject.name} records, so this one
|
|
1119
|
+
// goes under the ${subject.name} just created — and comes back off again.
|
|
1120
|
+
const ${child.name} = await seneca
|
|
1121
|
+
.entity('provider/${provider.lower}/${child.name}')
|
|
1122
|
+
.make$({ ${ckey}: id${'' === cmake ? '' : ', ' + cmake} })
|
|
1123
|
+
.save$()
|
|
1124
|
+
console.log('${child.name.toUpperCase()} CREATED', ${child.name})
|
|
1125
|
+
|
|
1126
|
+
await seneca
|
|
1127
|
+
.entity('provider/${provider.lower}/${child.name}')
|
|
1128
|
+
.remove$({ ${ckey}: id, ${cidf}: ${child.name}.${cidf} })
|
|
1129
|
+
console.log('${child.name.toUpperCase()} REMOVED')
|
|
1130
|
+
|
|
1131
|
+
`)
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
Content(` }
|
|
1135
|
+
finally {
|
|
1136
|
+
await seneca.entity('provider/${provider.lower}/${subject.name}').remove$(id)
|
|
1137
|
+
console.log('REMOVED', id)
|
|
1138
|
+
}
|
|
1139
|
+
`)
|
|
1140
|
+
if (subject.cmds.includes('load')) {
|
|
1141
|
+
Content(`
|
|
1142
|
+
console.log(
|
|
1143
|
+
'AFTER REMOVE (expect null)',
|
|
1144
|
+
await seneca.entity('provider/${provider.lower}/${subject.name}').load$(id)
|
|
1145
|
+
)
|
|
1146
|
+
`)
|
|
1147
|
+
}
|
|
1148
|
+
Content(`}
|
|
1149
|
+
`)
|
|
1150
|
+
})
|
|
1151
|
+
}
|
|
1152
|
+
})
|
|
1153
|
+
})
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
// --- .github/workflows/build.yml --------------------------------------------
|
|
1157
|
+
|
|
1158
|
+
const Workflow = cmp(function Workflow(props: any) {
|
|
1159
|
+
const { provider } = props
|
|
1160
|
+
|
|
1161
|
+
Folder({ name: '.github' }, () => {
|
|
1162
|
+
Folder({ name: 'workflows' }, () => {
|
|
1163
|
+
File({ name: 'build.yml' }, () => {
|
|
1164
|
+
Content(`# Generated by @voxgig/sdkgen. Do not edit.
|
|
1165
|
+
#
|
|
1166
|
+
# The ${provider.api} SDK is a normal published dependency, so \`npm install\`
|
|
1167
|
+
# is all that is needed to build and run the offline tests on every platform.
|
|
1168
|
+
${!provider.liveApp ? '' : `#
|
|
1169
|
+
# The live tests additionally need the companion server, which is only
|
|
1170
|
+
# distributed in the SDK's source repository (it is not published). That repo
|
|
1171
|
+
# is cloned and started on Linux only, because backgrounding the server
|
|
1172
|
+
# assumes a POSIX shell. The live tests probe for the server and skip cleanly
|
|
1173
|
+
# when it is absent, so on Windows and macOS they simply skip.`}
|
|
1174
|
+
|
|
1175
|
+
name: build
|
|
1176
|
+
|
|
1177
|
+
on:
|
|
1178
|
+
push:
|
|
1179
|
+
branches: [main]
|
|
1180
|
+
pull_request:
|
|
1181
|
+
branches: [main]
|
|
1182
|
+
|
|
1183
|
+
jobs:
|
|
1184
|
+
build:
|
|
1185
|
+
timeout-minutes: 10
|
|
1186
|
+
|
|
1187
|
+
strategy:
|
|
1188
|
+
fail-fast: false
|
|
1189
|
+
matrix:
|
|
1190
|
+
os: [ubuntu-latest, windows-latest, macos-latest]
|
|
1191
|
+
node-version: [24.x]
|
|
1192
|
+
|
|
1193
|
+
runs-on: \${{ matrix.os }}
|
|
1194
|
+
|
|
1195
|
+
steps:
|
|
1196
|
+
- uses: actions/checkout@v7
|
|
1197
|
+
|
|
1198
|
+
- name: Use Node.js \${{ matrix.node-version }}
|
|
1199
|
+
uses: actions/setup-node@v7
|
|
1200
|
+
with:
|
|
1201
|
+
node-version: \${{ matrix.node-version }}
|
|
1202
|
+
|
|
1203
|
+
${!provider.liveApp ? '' : `
|
|
1204
|
+
# Live-test target. Failure to start degrades coverage (the live tests
|
|
1205
|
+
# probe first and skip) rather than failing the build — which is what
|
|
1206
|
+
# continue-on-error is for, and what its absence undid: GitHub runs a
|
|
1207
|
+
# run: block under bash -e, so a repo whose app/ has no build script,
|
|
1208
|
+
# or no app/ at all, went red on its first push with the comment
|
|
1209
|
+
# above still claiming otherwise.
|
|
1210
|
+
- name: Start the ${provider.api} test server
|
|
1211
|
+
if: runner.os == 'Linux'
|
|
1212
|
+
continue-on-error: true
|
|
1213
|
+
run: |
|
|
1214
|
+
git clone --depth 1 \\
|
|
1215
|
+
${provider.sdkRepoUrl}.git \\
|
|
1216
|
+
"$RUNNER_TEMP/sdk"
|
|
1217
|
+
cd "$RUNNER_TEMP/sdk/app"
|
|
1218
|
+
npm install
|
|
1219
|
+
npm run build
|
|
1220
|
+
npm start &
|
|
1221
|
+
for i in $(seq 1 30); do
|
|
1222
|
+
if curl -sf ${provider.liveBase}${provider.probePath} > /dev/null; then
|
|
1223
|
+
echo "server up"
|
|
1224
|
+
exit 0
|
|
1225
|
+
fi
|
|
1226
|
+
sleep 1
|
|
1227
|
+
done
|
|
1228
|
+
echo "server did not start; live tests will skip"
|
|
1229
|
+
`}
|
|
1230
|
+
- run: npm install
|
|
1231
|
+
|
|
1232
|
+
# The Seneca host framework is a PEER dependency, so the test suite needs
|
|
1233
|
+
# it installed explicitly. --no-save keeps npm from rewriting the peer
|
|
1234
|
+
# ranges in package.json to carets on what it happened to resolve, which
|
|
1235
|
+
# would have the build testing a manifest the repo never authored.
|
|
1236
|
+
- run: npm i --no-save seneca seneca-entity seneca-promisify @seneca/provider @seneca/env
|
|
1237
|
+
|
|
1238
|
+
- run: npm run build --if-present
|
|
1239
|
+
- run: npm test
|
|
1240
|
+
`)
|
|
1241
|
+
})
|
|
1242
|
+
|
|
1243
|
+
// --- publish.yml ---------------------------------------------------
|
|
1244
|
+
//
|
|
1245
|
+
// Release on a `v*` tag push, via GitHub OIDC Trusted Publishing — no
|
|
1246
|
+
// NPM_TOKEN secret anywhere. `id-token: write` lets npm exchange a
|
|
1247
|
+
// GitHub OIDC token for a short-lived publish credential, and npm
|
|
1248
|
+
// attaches provenance automatically.
|
|
1249
|
+
//
|
|
1250
|
+
// TWO THINGS ARE LOAD-BEARING AND EASY TO GET WRONG.
|
|
1251
|
+
//
|
|
1252
|
+
// The FILENAME. npm's trusted publisher is registered against this
|
|
1253
|
+
// file's name, so renaming it breaks publishing until the npm-side
|
|
1254
|
+
// configuration is changed to match. It is publish.yml deliberately.
|
|
1255
|
+
//
|
|
1256
|
+
// `npm install`, NOT `npm ci`. A Seneca plugin does not commit its
|
|
1257
|
+
// lockfile (see .gitignore), so there is nothing for ci to install
|
|
1258
|
+
// from — it fails outright. The SDK repo commits one and uses ci; this
|
|
1259
|
+
// package cannot.
|
|
1260
|
+
//
|
|
1261
|
+
// The host framework is installed explicitly for the same reason
|
|
1262
|
+
// build.yml does it: seneca and its plugins are PEER dependencies, and
|
|
1263
|
+
// the test suite requires them directly.
|
|
1264
|
+
File({ name: 'publish.yml' }, () => {
|
|
1265
|
+
Content(`# Generated by @voxgig/sdkgen. Do not edit.
|
|
1266
|
+
#
|
|
1267
|
+
# Publishes ${provider.pkgName} to npm on a \`v*\` tag push, via GitHub OIDC
|
|
1268
|
+
# Trusted Publishing — no NPM_TOKEN secret. The \`id-token: write\` permission
|
|
1269
|
+
# lets npm exchange a GitHub OIDC token for a short-lived publish credential,
|
|
1270
|
+
# and provenance is attached automatically.
|
|
1271
|
+
#
|
|
1272
|
+
# The trusted publisher must be registered on npmjs.com for this package
|
|
1273
|
+
# against THIS filename (publish.yml); renaming this file breaks publishing
|
|
1274
|
+
# until the npm-side config is updated to match.
|
|
1275
|
+
#
|
|
1276
|
+
# npm cannot publish a package's FIRST version this way — the settings page
|
|
1277
|
+
# that configures a trusted publisher only exists once a version is there. So
|
|
1278
|
+
# release ${provider.version} by hand once, configure the publisher, and every
|
|
1279
|
+
# release after that is a tag push.
|
|
1280
|
+
#
|
|
1281
|
+
# Release flow: bump the version in the SDK model
|
|
1282
|
+
# (\`main: kit: target: 'seneca-provider': publish: version\`), regenerate,
|
|
1283
|
+
# merge, then push a v* tag.
|
|
1284
|
+
|
|
1285
|
+
name: publish
|
|
1286
|
+
|
|
1287
|
+
on:
|
|
1288
|
+
push:
|
|
1289
|
+
tags: ['v*']
|
|
1290
|
+
workflow_dispatch:
|
|
1291
|
+
|
|
1292
|
+
jobs:
|
|
1293
|
+
publish:
|
|
1294
|
+
name: npm publish
|
|
1295
|
+
runs-on: ubuntu-latest
|
|
1296
|
+
timeout-minutes: 15
|
|
1297
|
+
permissions:
|
|
1298
|
+
id-token: write
|
|
1299
|
+
contents: read
|
|
1300
|
+
|
|
1301
|
+
steps:
|
|
1302
|
+
- uses: actions/checkout@v4
|
|
1303
|
+
|
|
1304
|
+
- uses: actions/setup-node@v4
|
|
1305
|
+
with:
|
|
1306
|
+
node-version: 24.x
|
|
1307
|
+
registry-url: 'https://registry.npmjs.org'
|
|
1308
|
+
|
|
1309
|
+
# Trusted publishing requires npm >= 11.5.1.
|
|
1310
|
+
- name: Use a trusted-publishing capable npm
|
|
1311
|
+
run: npm install -g npm@latest
|
|
1312
|
+
|
|
1313
|
+
# install, not ci: this package does not commit a lockfile.
|
|
1314
|
+
- run: npm install
|
|
1315
|
+
|
|
1316
|
+
# The Seneca host framework is a PEER dependency, so the test suite
|
|
1317
|
+
# needs it installed explicitly.
|
|
1318
|
+
#
|
|
1319
|
+
# --no-save IS LOAD-BEARING. Without it npm rewrites the peer ranges in
|
|
1320
|
+
# package.json to carets on whatever it resolved, and \`npm publish\`
|
|
1321
|
+
# below then ships that rewritten manifest — so an authored \`>=26\`
|
|
1322
|
+
# reaches consumers as \`^28.1.0\` and the package refuses to install for
|
|
1323
|
+
# anyone on a newer major. The repo looks fine; only the artifact is
|
|
1324
|
+
# narrowed. Install into node_modules, leave the manifest alone.
|
|
1325
|
+
- run: npm i --no-save seneca seneca-entity seneca-promisify @seneca/provider @seneca/env
|
|
1326
|
+
|
|
1327
|
+
- run: npm run build
|
|
1328
|
+
- run: npm test
|
|
1329
|
+
|
|
1330
|
+
# The tag must match what the manifest declares, or a tag push silently
|
|
1331
|
+
# republishes whatever version happens to be in package.json.
|
|
1332
|
+
- name: Check the tag matches the manifest version
|
|
1333
|
+
run: |
|
|
1334
|
+
TAG="\${GITHUB_REF_NAME#v}"
|
|
1335
|
+
PKG=$(node -p "require('./package.json').version")
|
|
1336
|
+
if [ "$TAG" != "$PKG" ]; then
|
|
1337
|
+
echo "tag v$TAG does not match package.json $PKG"
|
|
1338
|
+
exit 1
|
|
1339
|
+
fi
|
|
1340
|
+
|
|
1341
|
+
- name: Publish to npm
|
|
1342
|
+
run: npm publish --access public
|
|
1343
|
+
`)
|
|
1344
|
+
})
|
|
1345
|
+
})
|
|
1346
|
+
})
|
|
1347
|
+
})
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
// --- README.md ---------------------------------------------------------------
|
|
1351
|
+
//
|
|
1352
|
+
// The heading set is NOT free: @seneca/maintain checks a Seneca plugin README
|
|
1353
|
+
// for "Quick Example", "More Examples", "Motivation", "Support", "API",
|
|
1354
|
+
// "Contributing" and "Background", and the generated `maintain` test fails
|
|
1355
|
+
// without them. That check is the reason to generate this file rather than
|
|
1356
|
+
// leave it to a maintainer.
|
|
1357
|
+
|
|
1358
|
+
const Readme = cmp(function Readme(props: any) {
|
|
1359
|
+
const { provider } = props
|
|
1360
|
+
|
|
1361
|
+
const subject = [...provider.entities]
|
|
1362
|
+
.sort((a: any, b: any) =>
|
|
1363
|
+
(a.parents.length - b.parents.length) || (b.cmds.length - a.cmds.length))[0]
|
|
1364
|
+
|
|
1365
|
+
const nested = provider.entities.filter((e: any) => 0 < e.parents.length)
|
|
1366
|
+
|
|
1367
|
+
File({ name: 'README.md' }, () => {
|
|
1368
|
+
Content(`
|
|
1369
|
+
|
|
1370
|
+
> _Seneca ${provider.Name}-Provider_ is a plugin for [Seneca](http://senecajs.org)
|
|
1371
|
+
|
|
1372
|
+
Provides access to the ${provider.api} API using the Seneca _provider_
|
|
1373
|
+
convention. ${provider.api} entities are represented as Seneca entities so that
|
|
1374
|
+
they can be accessed using the Seneca entity API and messages.
|
|
1375
|
+
|
|
1376
|
+
Requests are handled by the [${provider.api} SDK](${provider.sdkRepoUrl}),
|
|
1377
|
+
which is generated from the API's OpenAPI specification. This plugin is
|
|
1378
|
+
generated from the same specification by
|
|
1379
|
+
[@voxgig/sdkgen](https://github.com/voxgig/sdkgen) — do not edit it by hand,
|
|
1380
|
+
change the model and regenerate.
|
|
1381
|
+
|
|
1382
|
+
See [seneca-entity](https://github.com/senecajs/seneca-entity) and the [Seneca Data
|
|
1383
|
+
Entities
|
|
1384
|
+
Tutorial](https://senecajs.org/docs/tutorials/understanding-data-entities.html)
|
|
1385
|
+
for more details on the Seneca entity API.
|
|
1386
|
+
|
|
1387
|
+
[](${provider.repoUrl}/actions/workflows/build.yml)
|
|
1388
|
+
|
|
1389
|
+
| This open source module is sponsored and supported by [${provider.publisher}](${provider.publisherUrl}). |
|
|
1390
|
+
| --- |
|
|
1391
|
+
|
|
1392
|
+
|
|
1393
|
+
<!--START:SECTION:intro-->
|
|
1394
|
+
<!--END:SECTION:intro-->
|
|
1395
|
+
|
|
1396
|
+
|
|
1397
|
+
## Documentation
|
|
1398
|
+
|
|
1399
|
+
Full documentation lives in [\`doc/\`](doc/README.md) and follows the
|
|
1400
|
+
[Diátaxis](https://diataxis.fr) framework:
|
|
1401
|
+
|
|
1402
|
+
| Document | Purpose |
|
|
1403
|
+
| -------- | ------- |
|
|
1404
|
+
| [Tutorial](doc/tutorial.md) | Start here. Build a working script from an empty folder. |
|
|
1405
|
+
| [How-to guides](doc/how-to.md) | Recipes for specific tasks. |
|
|
1406
|
+
| [Reference](doc/reference.md) | Every pattern, entity, option and export. |
|
|
1407
|
+
| [Explanation](doc/explanation.md) | Why the plugin is designed this way. |
|
|
1408
|
+
|
|
1409
|
+
|
|
1410
|
+
## Quick Example
|
|
1411
|
+
|
|
1412
|
+
\`\`\`js
|
|
1413
|
+
const Seneca = require('seneca')
|
|
1414
|
+
|
|
1415
|
+
const seneca = Seneca()
|
|
1416
|
+
.use('promisify')
|
|
1417
|
+
.use('entity')
|
|
1418
|
+
.use('env', { var: { $${provider.ENV}_APIKEY: '' } })
|
|
1419
|
+
.use('provider', {
|
|
1420
|
+
provider: {
|
|
1421
|
+
${provider.lower}: {
|
|
1422
|
+
keys: { apikey: { value: '$${provider.ENV}_APIKEY' } },
|
|
1423
|
+
},
|
|
1424
|
+
},
|
|
1425
|
+
})
|
|
1426
|
+
.use('${provider.pkgName}')
|
|
1427
|
+
|
|
1428
|
+
await seneca.ready()
|
|
1429
|
+
|
|
1430
|
+
`)
|
|
1431
|
+
if (subject.cmds.includes('list')) {
|
|
1432
|
+
Content(`const ${subject.name}s = await seneca
|
|
1433
|
+
.entity('provider/${provider.lower}/${subject.name}').list$()
|
|
1434
|
+
`)
|
|
1435
|
+
}
|
|
1436
|
+
if (subject.cmds.includes('load')) {
|
|
1437
|
+
Content(`const ${subject.name} = await seneca
|
|
1438
|
+
.entity('provider/${provider.lower}/${subject.name}').load$('some-id')
|
|
1439
|
+
`)
|
|
1440
|
+
}
|
|
1441
|
+
Content(`\`\`\`
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
## Install
|
|
1445
|
+
|
|
1446
|
+
\`\`\`sh
|
|
1447
|
+
npm install ${provider.pkgName}
|
|
1448
|
+
\`\`\`
|
|
1449
|
+
|
|
1450
|
+
This plugin expects the Seneca host framework to be present:
|
|
1451
|
+
|
|
1452
|
+
\`\`\`sh
|
|
1453
|
+
npm install seneca seneca-entity seneca-promisify @seneca/provider @seneca/env
|
|
1454
|
+
\`\`\`
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
## Options
|
|
1458
|
+
|
|
1459
|
+
| Option | Type | Description |
|
|
1460
|
+
| --- | --- | --- |
|
|
1461
|
+
| \`sdk\` | object | Passed straight to the \`${provider.sdkClass}\` constructor. Most usefully \`base\`, to point at a server. |
|
|
1462
|
+
| \`test\` | boolean | Run the SDK in offline test mode (in-memory mock transport). |
|
|
1463
|
+
| \`testopts\` | object | Seed and options for the mock, used only when \`test\` is true. |
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
## Entities
|
|
1467
|
+
|
|
1468
|
+
Each API entity is exposed as a Seneca entity under
|
|
1469
|
+
\`provider/${provider.lower}/<entity>\`.
|
|
1470
|
+
|
|
1471
|
+
| Seneca entity | Commands | Fields |
|
|
1472
|
+
| --- | --- | --- |
|
|
1473
|
+
`)
|
|
1474
|
+
// Fields as well as commands: a reader deciding whether this plugin
|
|
1475
|
+
// covers what they need has to know what a record CONTAINS, and the
|
|
1476
|
+
// table used to answer only half the question.
|
|
1477
|
+
each(provider.entities, (e: any) => {
|
|
1478
|
+
const fields = 0 === e.fields.length ? '—' :
|
|
1479
|
+
e.fields.map((f: any) => '`' + f.name + '`').join(', ')
|
|
1480
|
+
Content(`| \`provider/${provider.lower}/${e.name}\` | ${e.cmds.map((c: string) => '`' + c + '$`').join(', ')} | ${fields} |
|
|
1481
|
+
`)
|
|
1482
|
+
})
|
|
1483
|
+
|
|
1484
|
+
if (0 < nested.length) {
|
|
1485
|
+
Content(`
|
|
1486
|
+
### Nested entities
|
|
1487
|
+
|
|
1488
|
+
Some entities live under a parent in the API path, so every command needs the
|
|
1489
|
+
parent's id in the query. Leaving it out throws with a message naming the
|
|
1490
|
+
missing key, rather than failing as an opaque 404 from a half-built URL.
|
|
1491
|
+
|
|
1492
|
+
`)
|
|
1493
|
+
each(nested, (e: any) => {
|
|
1494
|
+
Content(`- \`${e.name}\` requires \`${e.parents.join('`, `')}\`
|
|
1495
|
+
`)
|
|
1496
|
+
})
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
// CUSTOM ACTIONS.
|
|
1500
|
+
//
|
|
1501
|
+
// apidef folds a non-CRUD verb into an ordinary op as an alternative
|
|
1502
|
+
// point, and the SDK reaches it with `$action` in the call's argument.
|
|
1503
|
+
// The `ts` target documents this in its own REFERENCE.md and the same
|
|
1504
|
+
// treatment belongs here, because the Seneca spelling is DIFFERENT
|
|
1505
|
+
// (`action$`, trailing dollar, Seneca's directive convention) and a
|
|
1506
|
+
// reader who has only ever seen the SDK's would guess wrong.
|
|
1507
|
+
//
|
|
1508
|
+
// Undocumented, this is the state the plugin was in before: a GitHub
|
|
1509
|
+
// provider with a `pull` entity and no way to merge a pull request at
|
|
1510
|
+
// all, because nothing anywhere said the endpoint existed.
|
|
1511
|
+
const acting = provider.entities.filter((e: any) => 0 < e.actionList.length)
|
|
1512
|
+
|
|
1513
|
+
if (0 < acting.length) {
|
|
1514
|
+
const first = acting[0]
|
|
1515
|
+
const firstAct = first.actionList[0]
|
|
1516
|
+
const saver = acting.find((e: any) =>
|
|
1517
|
+
e.actionList.some((a: any) => 'save' === a.cmd))
|
|
1518
|
+
|
|
1519
|
+
Content(`
|
|
1520
|
+
### Actions
|
|
1521
|
+
|
|
1522
|
+
Some API endpoints are not one of the five CRUD operations — merging a pull
|
|
1523
|
+
request, uploading an image. The API definition folds each one into an
|
|
1524
|
+
ordinary operation as an alternative route, and this plugin selects one with
|
|
1525
|
+
the \`action$\` directive, alongside Seneca's own \`sort$\`, \`limit$\` and
|
|
1526
|
+
\`fields$\`.
|
|
1527
|
+
|
|
1528
|
+
| Entity | Action | Route | Command |
|
|
1529
|
+
| --- | --- | --- | --- |
|
|
1530
|
+
`)
|
|
1531
|
+
each(acting, (e: any) => {
|
|
1532
|
+
each(e.actionList, (a: any) => {
|
|
1533
|
+
Content(`| \`${e.name}\` | \`${a.action}\` | \`${a.path}\` | \`${a.cmd}$\` |
|
|
1534
|
+
`)
|
|
1535
|
+
})
|
|
1536
|
+
})
|
|
1537
|
+
|
|
1538
|
+
Content(`
|
|
1539
|
+
An action returns that action's OWN response, which is not necessarily a
|
|
1540
|
+
record of the entity it hangs off — check the API definition for its shape.
|
|
1541
|
+
Naming an action the entity does not have throws, and names the ones it
|
|
1542
|
+
does have. It never falls back to the plain command.
|
|
1543
|
+
|
|
1544
|
+
`)
|
|
1545
|
+
|
|
1546
|
+
if (null != saver) {
|
|
1547
|
+
const act = saver.actionList.find((a: any) => 'save' === a.cmd)
|
|
1548
|
+
Content(`On \`save$\`, pass it as a directive. The rest of the entity is the
|
|
1549
|
+
action's payload:
|
|
1550
|
+
|
|
1551
|
+
\`\`\`js
|
|
1552
|
+
const ${saver.name} = seneca.entity('provider/${provider.lower}/${saver.name}')
|
|
1553
|
+
|
|
1554
|
+
await ${saver.name}
|
|
1555
|
+
.make$({ id: 'some-id', /* ...the action's own arguments */ })
|
|
1556
|
+
.directive$({ action$: '${act.action}' })
|
|
1557
|
+
.save$()
|
|
1558
|
+
\`\`\`
|
|
1559
|
+
|
|
1560
|
+
> **\`make$({ action$: '${act.action}' })\` does not work**, and cannot.
|
|
1561
|
+
> \`seneca-entity\`'s \`make$\` copies only keys without a \`$\`, plus the four
|
|
1562
|
+
> directives it knows by name (\`id$\`, \`merge$\`, \`custom$\`, \`directive$\`),
|
|
1563
|
+
> so any other trailing-\`$\` key is dropped before this plugin sees it —
|
|
1564
|
+
> there is nothing left for it to refuse. Use \`directive$\` as above, or
|
|
1565
|
+
> assign the property to an entity you already made:
|
|
1566
|
+
>
|
|
1567
|
+
> \`\`\`js
|
|
1568
|
+
> const p = ${saver.name}.make$({ id: 'some-id' })
|
|
1569
|
+
> p.action$ = '${act.action}'
|
|
1570
|
+
> await p.save$()
|
|
1571
|
+
> \`\`\`
|
|
1572
|
+
|
|
1573
|
+
`)
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
if ('save' !== firstAct.cmd) {
|
|
1577
|
+
Content(`On \`${firstAct.cmd}$\`, pass it in the query:
|
|
1578
|
+
|
|
1579
|
+
\`\`\`js
|
|
1580
|
+
await seneca.entity('provider/${provider.lower}/${first.name}')
|
|
1581
|
+
.${firstAct.cmd}$({ action$: '${firstAct.action}' })
|
|
1582
|
+
\`\`\`
|
|
1583
|
+
|
|
1584
|
+
`)
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
Content(`
|
|
1589
|
+
|
|
1590
|
+
## Action Patterns
|
|
1591
|
+
|
|
1592
|
+
Every message pattern this plugin registers. The entity actions are the ones
|
|
1593
|
+
\`seneca-entity\` dispatches to when you call \`list$\` / \`load$\` / \`save$\` /
|
|
1594
|
+
\`remove$\` on a canon below — you rarely post them by hand, but they are what
|
|
1595
|
+
appears in a Seneca log, and a plugin that documents one of nine is a plugin
|
|
1596
|
+
whose logs cannot be read.
|
|
1597
|
+
|
|
1598
|
+
| Pattern | Description |
|
|
1599
|
+
| --- | --- |
|
|
1600
|
+
| \`sys:provider,provider:${provider.lower},get:info\` | Plugin and SDK version information. |
|
|
1601
|
+
`)
|
|
1602
|
+
|
|
1603
|
+
const CMD_DESC: Record<string, string> = {
|
|
1604
|
+
list: 'List records',
|
|
1605
|
+
load: 'Load one record',
|
|
1606
|
+
save: 'Create or update a record',
|
|
1607
|
+
remove: 'Remove a record',
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
each(provider.entities, (e: any) => {
|
|
1611
|
+
each(e.cmds, (cmd: any) => {
|
|
1612
|
+
const c = String(cmd.val$ ?? cmd)
|
|
1613
|
+
Content(`| \`sys:entity,cmd:${c},zone:provider,base:${provider.lower},name:${e.name}\` | ${CMD_DESC[c]}. |
|
|
1614
|
+
`)
|
|
1615
|
+
})
|
|
1616
|
+
})
|
|
1617
|
+
|
|
1618
|
+
Content(`
|
|
1619
|
+
|
|
1620
|
+
|
|
1621
|
+
## More Examples
|
|
1622
|
+
|
|
1623
|
+
### Offline testing
|
|
1624
|
+
|
|
1625
|
+
The SDK ships an in-memory mock transport, so this plugin can be exercised
|
|
1626
|
+
with no server:
|
|
1627
|
+
|
|
1628
|
+
\`\`\`js
|
|
1629
|
+
.use('${provider.pkgName}', { test: true, testopts: { entity: { ... } } })
|
|
1630
|
+
\`\`\`
|
|
1631
|
+
|
|
1632
|
+
\`testopts\` is passed straight to the SDK's test constructor; \`entity\`
|
|
1633
|
+
seeds the mock store. See \`test/seed.js\` for the shape.
|
|
1634
|
+
|
|
1635
|
+
`)
|
|
1636
|
+
if ('' !== provider.liveBase) {
|
|
1637
|
+
Content(`### Running against a server
|
|
1638
|
+
|
|
1639
|
+
\`\`\`js
|
|
1640
|
+
.use('${provider.pkgName}', { sdk: { base: '${provider.liveBase}' } })
|
|
1641
|
+
\`\`\`
|
|
1642
|
+
|
|
1643
|
+
The companion test server is distributed in the SDK's source repository
|
|
1644
|
+
only. From a checkout beside this one:
|
|
1645
|
+
|
|
1646
|
+
\`\`\`sh
|
|
1647
|
+
cd ${provider.sdkrel}/app && npm start
|
|
1648
|
+
\`\`\`
|
|
1649
|
+
|
|
1650
|
+
Then \`node test/live.js\` reads from it, and \`node test/quick.js\` runs a
|
|
1651
|
+
full create/update/load/remove cycle.
|
|
1652
|
+
|
|
1653
|
+
`)
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
Content(`
|
|
1657
|
+
## Motivation
|
|
1658
|
+
|
|
1659
|
+
Applications rarely talk to one external service, and each service usually
|
|
1660
|
+
arrives with its own client library, authentication style and error
|
|
1661
|
+
conventions. That variety leaks into application code and makes it harder to
|
|
1662
|
+
test.
|
|
1663
|
+
|
|
1664
|
+
The Seneca provider convention removes the variety: every external service
|
|
1665
|
+
becomes a Seneca entity reached with \`list$\`, \`load$\`, \`save$\` and
|
|
1666
|
+
\`remove$\`, so application code has one shape regardless of what it talks to.
|
|
1667
|
+
|
|
1668
|
+
The SDK underneath arrives at a similar conclusion from the other side — it
|
|
1669
|
+
deliberately exposes entities rather than HTTP routes. This plugin is the
|
|
1670
|
+
short bridge between the two.
|
|
1671
|
+
|
|
1672
|
+
|
|
1673
|
+
## Support
|
|
1674
|
+
|
|
1675
|
+
- Issues and bugs: [GitHub issues](${provider.repoUrl}/issues)
|
|
1676
|
+
- Seneca community: [senecajs.org](http://senecajs.org)
|
|
1677
|
+
|
|
1678
|
+
|
|
1679
|
+
## API
|
|
1680
|
+
|
|
1681
|
+
### Plugin export: \`${provider.pluginName}/sdk\`
|
|
1682
|
+
|
|
1683
|
+
Returns the configured \`${provider.sdkClass}\` instance, for the operations
|
|
1684
|
+
the entity API does not cover:
|
|
1685
|
+
|
|
1686
|
+
\`\`\`js
|
|
1687
|
+
const sdk = seneca.export('${provider.pluginName}/sdk')()
|
|
1688
|
+
\`\`\`
|
|
1689
|
+
|
|
1690
|
+
|
|
1691
|
+
## Contributing
|
|
1692
|
+
|
|
1693
|
+
This plugin is GENERATED. Changes belong in the SDK project's model and
|
|
1694
|
+
components, not here — anything edited in this repository is overwritten by
|
|
1695
|
+
the next generation run.
|
|
1696
|
+
|
|
1697
|
+
The [Senecajs org](http://senecajs.org) encourages open participation. If you
|
|
1698
|
+
feel you can help in any way, be it with bug reporting, documentation,
|
|
1699
|
+
examples, extra testing, or new features, please get in touch.
|
|
1700
|
+
|
|
1701
|
+
|
|
1702
|
+
## Background
|
|
1703
|
+
|
|
1704
|
+
Generated by [@voxgig/sdkgen](https://github.com/voxgig/sdkgen) from the
|
|
1705
|
+
${provider.api} API definition, against the
|
|
1706
|
+
[${provider.sdkPkg}](https://www.npmjs.com/package/${provider.sdkPkg}) SDK.
|
|
1707
|
+
`)
|
|
1708
|
+
})
|
|
1709
|
+
})
|
|
1710
|
+
|
|
1711
|
+
|
|
1712
|
+
// --- doc/tutorial.md ---------------------------------------------------------
|
|
1713
|
+
//
|
|
1714
|
+
// The Diataxis TUTORIAL: an empty folder to a working script in about fifteen
|
|
1715
|
+
// minutes. It teaches, so it is deliberately narrower than the other three
|
|
1716
|
+
// documents — one path, no alternatives, and no decisions asked of the
|
|
1717
|
+
// reader.
|
|
1718
|
+
//
|
|
1719
|
+
// Two decisions shape this component.
|
|
1720
|
+
//
|
|
1721
|
+
// FIRST, a tutorial must never ask the reader to invent a value. Every id in
|
|
1722
|
+
// the script is therefore either seeded here (offline) or read back from a
|
|
1723
|
+
// list call (live) — never a literal that only happens to exist on the
|
|
1724
|
+
// author's machine. That is also why a declared server is not by itself
|
|
1725
|
+
// enough to choose the live lesson: the primary entity must be listable, and
|
|
1726
|
+
// a nested primary entity must have a listable parent, or there is no honest
|
|
1727
|
+
// way to come by the first id. Failing that the offline lesson runs, which is
|
|
1728
|
+
// a complete tutorial in its own right rather than an apology for a missing
|
|
1729
|
+
// server.
|
|
1730
|
+
//
|
|
1731
|
+
// SECOND, the step numbers are computed rather than written, because which
|
|
1732
|
+
// steps exist depends on which cmds the model declares. `step()` counts as it
|
|
1733
|
+
// emits, and the prose refers to what a step did rather than to a number that
|
|
1734
|
+
// may not be there.
|
|
1735
|
+
//
|
|
1736
|
+
// The offline seed reuses seedRecord() — the same function behind
|
|
1737
|
+
// test/seed.js — so what the reader is told to paste has the shape the SDK
|
|
1738
|
+
// really answers with.
|
|
1739
|
+
|
|
1740
|
+
const DocTutorial = cmp(function DocTutorial(props: any) {
|
|
1741
|
+
const { provider } = props
|
|
1742
|
+
|
|
1743
|
+
// Entity and field names come from an API definition, so they cannot be
|
|
1744
|
+
// assumed to be legal JavaScript identifiers.
|
|
1745
|
+
const ident = (s: string) => String(s).replace(/[^A-Za-z0-9_$]/g, '_')
|
|
1746
|
+
const qkey = (k: string) =>
|
|
1747
|
+
/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(String(k)) ? String(k) : `'${k}'`
|
|
1748
|
+
const plural = (n: string) =>
|
|
1749
|
+
n.endsWith('s') ? `${ident(n)}List` : `${ident(n)}s`
|
|
1750
|
+
const canon = (n: string) => `provider/${provider.lower}/${n}`
|
|
1751
|
+
const entOf = (name: string) =>
|
|
1752
|
+
provider.entities.find((e: any) => e.name === name) || null
|
|
1753
|
+
|
|
1754
|
+
// The entity the lesson is built on: fewest parent keys to arrange, then
|
|
1755
|
+
// most cmds to show. The same choice the tests and the README make, so all
|
|
1756
|
+
// three talk about the same thing.
|
|
1757
|
+
const subject = [...provider.entities]
|
|
1758
|
+
.sort((a: any, b: any) =>
|
|
1759
|
+
(a.parents.length - b.parents.length) || (b.cmds.length - a.cmds.length))[0]
|
|
1760
|
+
|
|
1761
|
+
const idf = subject.idf || 'id'
|
|
1762
|
+
const subjParent = 0 < subject.parents.length ?
|
|
1763
|
+
entOf(subject.parentEntity) : null
|
|
1764
|
+
|
|
1765
|
+
const hasServer = '' !== provider.liveBase
|
|
1766
|
+
|
|
1767
|
+
// Can the live lesson actually be written? It needs a server AND a first id
|
|
1768
|
+
// the script can discover for itself.
|
|
1769
|
+
const live = hasServer &&
|
|
1770
|
+
subject.cmds.includes('list') &&
|
|
1771
|
+
(0 === subject.parents.length ||
|
|
1772
|
+
(1 === subject.parents.length &&
|
|
1773
|
+
null != subjParent && subjParent.cmds.includes('list')))
|
|
1774
|
+
|
|
1775
|
+
const offline = !live
|
|
1776
|
+
|
|
1777
|
+
// The nested entity the lesson finishes on. Prefer one hanging off the
|
|
1778
|
+
// subject, so the reader recognises the parent id when it turns up.
|
|
1779
|
+
const readable = (e: any) => e.cmds.includes('list') || e.cmds.includes('load')
|
|
1780
|
+
const nested = provider.entities
|
|
1781
|
+
.filter((e: any) => 0 < e.parents.length && e.name !== subject.name)
|
|
1782
|
+
.filter(readable)
|
|
1783
|
+
// Live, the parent id has to come from somewhere: exactly one parent key,
|
|
1784
|
+
// and a parent that can be listed.
|
|
1785
|
+
.filter((e: any) => offline ||
|
|
1786
|
+
(1 === e.parents.length && null != entOf(e.parentEntity) &&
|
|
1787
|
+
entOf(e.parentEntity).cmds.includes('list')))
|
|
1788
|
+
const child = nested.find((e: any) => e.parentEntity === subject.name) ||
|
|
1789
|
+
nested.find((e: any) => null != entOf(e.parentEntity)) ||
|
|
1790
|
+
nested[0] || null
|
|
1791
|
+
const childParent = null == child ? null : entOf(child.parentEntity)
|
|
1792
|
+
|
|
1793
|
+
// The value seedRecord() gives a parent key, so a query written here finds
|
|
1794
|
+
// the seeded record instead of quietly matching nothing.
|
|
1795
|
+
const seedParentVal = (e: any, k: string) => {
|
|
1796
|
+
const f = (e.fields || []).find((f: any) => f.name === k)
|
|
1797
|
+
const pe = (null != f && '' !== f.parentEntity) ? f.parentEntity :
|
|
1798
|
+
(k === e.parents[0] ? (e.parentEntity || '') : '')
|
|
1799
|
+
return `${pe}0`
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
// A seed record guaranteed to carry its id and its parent keys.
|
|
1803
|
+
// seedRecord() emits only the fields the model marks required, and a record
|
|
1804
|
+
// missing its parent key is invisible to the very query this lesson makes.
|
|
1805
|
+
const demoRecord = (e: any, idx: number) => {
|
|
1806
|
+
const rec: any = seedRecord(e, idx)
|
|
1807
|
+
const eidf = e.idf || 'id'
|
|
1808
|
+
if (null == rec[eidf]) {
|
|
1809
|
+
rec[eidf] = `${e.name}${idx}`
|
|
1810
|
+
}
|
|
1811
|
+
for (const k of e.parents) {
|
|
1812
|
+
if (null == rec[k]) {
|
|
1813
|
+
rec[k] = seedParentVal(e, k)
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
return rec
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
// Only the entities the lesson touches, in model order.
|
|
1820
|
+
const seedNames = [subject.name]
|
|
1821
|
+
if (null != subjParent) seedNames.push(subjParent.name)
|
|
1822
|
+
if (null != child) seedNames.push(child.name)
|
|
1823
|
+
if (null != childParent) seedNames.push(childParent.name)
|
|
1824
|
+
|
|
1825
|
+
const seedLiteral = provider.entities
|
|
1826
|
+
.filter((e: any) => seedNames.includes(e.name))
|
|
1827
|
+
.map((e: any) =>
|
|
1828
|
+
` ${qkey(e.name)}: {\n` +
|
|
1829
|
+
[0, 1].map((idx: number) =>
|
|
1830
|
+
` ${qkey(e.name + idx)}: ${JSON.stringify(demoRecord(e, idx))},\n`)
|
|
1831
|
+
.join('') +
|
|
1832
|
+
` },\n`)
|
|
1833
|
+
.join('')
|
|
1834
|
+
|
|
1835
|
+
// Every call on the subject carries its parent keys, if it has any: live
|
|
1836
|
+
// they come from a lookup, offline from the seed.
|
|
1837
|
+
const subjKeys = subject.parents.map((k: string) =>
|
|
1838
|
+
`${qkey(k)}: ${offline ? `'${seedParentVal(subject, k)}'` : ident(k)}`)
|
|
1839
|
+
const listArg = 0 === subjKeys.length ? '' : `{ ${subjKeys.join(', ')} }`
|
|
1840
|
+
const oneArg = (id: string) => 0 === subjKeys.length ? id :
|
|
1841
|
+
`{ ${subjKeys.concat([`${qkey(idf)}: ${id}`]).join(', ')} }`
|
|
1842
|
+
|
|
1843
|
+
const subjVar = plural(subject.name)
|
|
1844
|
+
const subjOne = ident(subject.name)
|
|
1845
|
+
|
|
1846
|
+
// Live, the first id is whatever the server answered with; offline it is
|
|
1847
|
+
// seeded above.
|
|
1848
|
+
const firstId = offline ? `'${subject.name}0'` : `${subjVar}[0].${idf}`
|
|
1849
|
+
|
|
1850
|
+
// A nested subject needs its parent's id before anything else can run.
|
|
1851
|
+
const subjPre = (live && null != subjParent) ?
|
|
1852
|
+
` // ${subject.name} records live under ${subjParent.name} records in the API,
|
|
1853
|
+
// so every ${subject.name} call needs a ${subject.parents[0]}.
|
|
1854
|
+
const ${plural(subjParent.name)} = await seneca
|
|
1855
|
+
.entity('${canon(subjParent.name)}')
|
|
1856
|
+
.list$()
|
|
1857
|
+
const ${ident(subject.parents[0])} = ${plural(subjParent.name)}[0].${subjParent.idf || 'id'}
|
|
1858
|
+
|
|
1859
|
+
` : ''
|
|
1860
|
+
|
|
1861
|
+
// Fields worth printing, and worth writing: not the id, not a parent key.
|
|
1862
|
+
const plainFields = subject.fields.filter((f: any) =>
|
|
1863
|
+
f.name !== idf && 'id' !== f.name && !subject.parents.includes(f.name))
|
|
1864
|
+
const shown = plainFields.slice(0, 2)
|
|
1865
|
+
const litval = (f: any, alt: boolean) =>
|
|
1866
|
+
'number' === f.kind ? (alt ? '4321' : '1234') :
|
|
1867
|
+
'boolean' === f.kind ? (alt ? 'true' : 'false') :
|
|
1868
|
+
`'tutorial-${f.name}${alt ? '-2' : ''}'`
|
|
1869
|
+
|
|
1870
|
+
const makeFields = subject.parents
|
|
1871
|
+
.map((k: string) =>
|
|
1872
|
+
`${qkey(k)}: ${offline ? `'${seedParentVal(subject, k)}'` : ident(k)}`)
|
|
1873
|
+
.concat(plainFields.map((f: any) => `${qkey(f.name)}: ${litval(f, false)}`))
|
|
1874
|
+
|
|
1875
|
+
const hasRead = subject.cmds.includes('list') || subject.cmds.includes('load')
|
|
1876
|
+
// Creating a record with nothing in it teaches nothing, so the write step
|
|
1877
|
+
// needs at least one field the caller actually supplies.
|
|
1878
|
+
const canWrite = subject.cmds.includes('save') && 0 < plainFields.length
|
|
1879
|
+
const canUpdate = canWrite && subject.ops.includes('update')
|
|
1880
|
+
const canRemove = canWrite && subject.cmds.includes('remove')
|
|
1881
|
+
|
|
1882
|
+
const cmdList = subject.cmds.map((c: string) => '`' + c + '$`').join(', ')
|
|
1883
|
+
|
|
1884
|
+
// Entity names are lowercase, and some of them have to start a sentence.
|
|
1885
|
+
const cap = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
|
|
1886
|
+
|
|
1887
|
+
// A nested SUBJECT would otherwise carry an unexplained parent key through
|
|
1888
|
+
// every example in the lesson. Said once, before the first call.
|
|
1889
|
+
const nestedNote = 0 === subject.parents.length ? '' :
|
|
1890
|
+
`${cap(subject.name)} records live inside ${'' === subject.parentEntity ?
|
|
1891
|
+
'a parent record' : `${subject.parentEntity} records`} in the API,
|
|
1892
|
+
and the route says so:
|
|
1893
|
+
|
|
1894
|
+
\`${subject.path}\`
|
|
1895
|
+
|
|
1896
|
+
The parent id there is not optional, so every ${subject.name} call
|
|
1897
|
+
carries ${subject.parents.map((k: string) => '`' + k + '`').join(' and ')} in its query. Leave it out and the provider
|
|
1898
|
+
names the key you missed, rather than letting a half-built URL come
|
|
1899
|
+
back as a puzzling 404.
|
|
1900
|
+
|
|
1901
|
+
`
|
|
1902
|
+
|
|
1903
|
+
// The clone lands in a directory named for the repository, so the cd that
|
|
1904
|
+
// follows can be exact rather than "wherever you put it". With no repo url
|
|
1905
|
+
// to clone from, the only path anyone can be told is the relative one back
|
|
1906
|
+
// to the SDK project.
|
|
1907
|
+
const sdkRepo = String(provider.sdkRepoUrl || '')
|
|
1908
|
+
const sdkDir = sdkRepo.replace(/\/+$/, '').split('/').pop() || 'sdk'
|
|
1909
|
+
const appDir = '' === sdkRepo ? `${provider.sdkrel}/app` : `${sdkDir}/app`
|
|
1910
|
+
|
|
1911
|
+
const getServer = '' === sdkRepo ?
|
|
1912
|
+
`You also need a server to talk to. The SDK itself installs from npm,
|
|
1913
|
+
but its test server does not — it ships only in the SDK's source
|
|
1914
|
+
project, in its \`app\` folder, which is at \`${provider.sdkrel}\`
|
|
1915
|
+
relative to this one.
|
|
1916
|
+
` :
|
|
1917
|
+
`You also need a server to talk to. The SDK itself installs from npm,
|
|
1918
|
+
but its test server does not — it ships only in the SDK's source
|
|
1919
|
+
repository, so clone that:
|
|
1920
|
+
|
|
1921
|
+
\`\`\`sh
|
|
1922
|
+
$ git clone ${sdkRepo}.git
|
|
1923
|
+
\`\`\`
|
|
1924
|
+
|
|
1925
|
+
If you already have that checkout beside this plugin, it is at
|
|
1926
|
+
\`${provider.sdkrel}\`, and you can skip the clone.
|
|
1927
|
+
`
|
|
1928
|
+
|
|
1929
|
+
// What a bare GET on the probe route answers with, when the model has one.
|
|
1930
|
+
const probeEnt = '' === provider.probePath ? null :
|
|
1931
|
+
provider.entities.find((e: any) => e.path === provider.probePath)
|
|
1932
|
+
|
|
1933
|
+
// Who assigns ids and holds the data, in prose.
|
|
1934
|
+
const source = live ? 'server' : 'store'
|
|
1935
|
+
|
|
1936
|
+
// Where the nested-subject note lands: the first step that shows a call.
|
|
1937
|
+
const noteAt = subject.cmds.includes('list') ? 'list' :
|
|
1938
|
+
subject.cmds.includes('load') ? 'load' : 'write'
|
|
1939
|
+
|
|
1940
|
+
let stepno = 0
|
|
1941
|
+
const step = (title: string) => `## Step ${++stepno}: ${title}`
|
|
1942
|
+
|
|
1943
|
+
File({ name: 'tutorial.md' }, () => {
|
|
1944
|
+
|
|
1945
|
+
Content(`# Tutorial: your first ${provider.Name} query
|
|
1946
|
+
|
|
1947
|
+
This tutorial takes you from an empty folder to a script that
|
|
1948
|
+
${canWrite ? 'reads and writes' : 'reads'} ${provider.api} data through
|
|
1949
|
+
Seneca entities. It should take about fifteen minutes.
|
|
1950
|
+
|
|
1951
|
+
You will build one script and add to it as you go.`)
|
|
1952
|
+
|
|
1953
|
+
if (live) {
|
|
1954
|
+
Content(` Everything runs
|
|
1955
|
+
locally against a test server you start yourself, so nothing here can
|
|
1956
|
+
affect anything outside your machine.
|
|
1957
|
+
|
|
1958
|
+
You need [Node.js](https://nodejs.org) 24 or later.
|
|
1959
|
+
|
|
1960
|
+
${getServer}
|
|
1961
|
+
${step('Start the test server')}
|
|
1962
|
+
|
|
1963
|
+
That server implements the ${provider.api} API. Build and start it:
|
|
1964
|
+
|
|
1965
|
+
\`\`\`sh
|
|
1966
|
+
$ cd ${appDir}
|
|
1967
|
+
$ npm install
|
|
1968
|
+
$ npm run build
|
|
1969
|
+
$ npm start
|
|
1970
|
+
\`\`\`
|
|
1971
|
+
|
|
1972
|
+
It listens on \`${provider.liveBase}\`. Check it from another terminal:
|
|
1973
|
+
|
|
1974
|
+
\`\`\`sh
|
|
1975
|
+
$ curl ${provider.liveBase}${provider.probePath}
|
|
1976
|
+
\`\`\`
|
|
1977
|
+
|
|
1978
|
+
`)
|
|
1979
|
+
Content(null == probeEnt ?
|
|
1980
|
+
`You should get a JSON answer rather than a refused connection.
|
|
1981
|
+
Leave the server running.
|
|
1982
|
+
|
|
1983
|
+
` :
|
|
1984
|
+
`You should see a JSON array of ${probeEnt.name} records.
|
|
1985
|
+
Leave the server running.
|
|
1986
|
+
|
|
1987
|
+
`)
|
|
1988
|
+
}
|
|
1989
|
+
else {
|
|
1990
|
+
Content(` Everything runs in
|
|
1991
|
+
memory: the SDK ships an offline mode backed by a small in-memory
|
|
1992
|
+
store, and you supply that store's contents yourself. No request leaves
|
|
1993
|
+
your machine, so nothing here can affect anything outside it.
|
|
1994
|
+
|
|
1995
|
+
You need [Node.js](https://nodejs.org) 24 or later. You do not need a
|
|
1996
|
+
server, a network connection, or credentials.
|
|
1997
|
+
|
|
1998
|
+
`)
|
|
1999
|
+
}
|
|
2000
|
+
|
|
2001
|
+
Content(`${step('Create the project')}
|
|
2002
|
+
|
|
2003
|
+
`)
|
|
2004
|
+
if (live) {
|
|
2005
|
+
Content(`In a new terminal:
|
|
2006
|
+
|
|
2007
|
+
`)
|
|
2008
|
+
}
|
|
2009
|
+
Content(`\`\`\`sh
|
|
2010
|
+
$ mkdir ${provider.lower}-demo
|
|
2011
|
+
$ cd ${provider.lower}-demo
|
|
2012
|
+
$ npm init -y
|
|
2013
|
+
$ npm install seneca seneca-entity seneca-promisify @seneca/provider ${provider.pkgName}
|
|
2014
|
+
\`\`\`
|
|
2015
|
+
|
|
2016
|
+
The first four are the Seneca host: the framework itself, the entity
|
|
2017
|
+
API, the promise wrapper that makes calls awaitable, and the shared
|
|
2018
|
+
machinery every Seneca provider is built on. The last is this plugin,
|
|
2019
|
+
which brings the ${provider.api} SDK with it.
|
|
2020
|
+
|
|
2021
|
+
${step('Connect')}
|
|
2022
|
+
|
|
2023
|
+
Create \`demo.js\`:
|
|
2024
|
+
|
|
2025
|
+
\`\`\`js
|
|
2026
|
+
const Seneca = require('seneca')
|
|
2027
|
+
|
|
2028
|
+
`)
|
|
2029
|
+
|
|
2030
|
+
if (offline) {
|
|
2031
|
+
Content(`// The offline store. Each key under an entity name is that record's
|
|
2032
|
+
// id, and each record is what the API would have answered with.
|
|
2033
|
+
const SEED = {
|
|
2034
|
+
entity: {
|
|
2035
|
+
${seedLiteral} },
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
`)
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
Content(`async function main() {
|
|
2042
|
+
const seneca = await Seneca({ legacy: false })
|
|
2043
|
+
.use('promisify')
|
|
2044
|
+
.use('entity')
|
|
2045
|
+
.use('provider', {
|
|
2046
|
+
provider: {
|
|
2047
|
+
${qkey(provider.lower)}: {
|
|
2048
|
+
keys: {
|
|
2049
|
+
apikey: { value: '' },
|
|
2050
|
+
},
|
|
2051
|
+
},
|
|
2052
|
+
},
|
|
2053
|
+
})
|
|
2054
|
+
`)
|
|
2055
|
+
Content(live ?
|
|
2056
|
+
` .use('${provider.pkgName}', {
|
|
2057
|
+
sdk: { base: '${provider.liveBase}' },
|
|
2058
|
+
})
|
|
2059
|
+
` :
|
|
2060
|
+
` .use('${provider.pkgName}', {
|
|
2061
|
+
test: true,
|
|
2062
|
+
testopts: SEED,
|
|
2063
|
+
})
|
|
2064
|
+
`)
|
|
2065
|
+
Content(` .ready()
|
|
2066
|
+
|
|
2067
|
+
const info = await seneca.post('sys:provider,provider:${provider.lower},get:info')
|
|
2068
|
+
console.log(info)
|
|
2069
|
+
}
|
|
2070
|
+
|
|
2071
|
+
main()
|
|
2072
|
+
\`\`\`
|
|
2073
|
+
|
|
2074
|
+
Run it:
|
|
2075
|
+
|
|
2076
|
+
\`\`\`sh
|
|
2077
|
+
$ node demo.js
|
|
2078
|
+
\`\`\`
|
|
2079
|
+
|
|
2080
|
+
You should see:
|
|
2081
|
+
|
|
2082
|
+
\`\`\`js
|
|
2083
|
+
{
|
|
2084
|
+
ok: true,
|
|
2085
|
+
name: '${provider.lower}',
|
|
2086
|
+
version: '${provider.version}',
|
|
2087
|
+
sdk: { name: '${provider.sdkPkg}', version: '${provider.sdkVersion}' },
|
|
2088
|
+
}
|
|
2089
|
+
\`\`\`
|
|
2090
|
+
|
|
2091
|
+
Two details of that configuration are worth a moment. The \`apikey\` is
|
|
2092
|
+
declared even though nothing here asks for credentials — an empty
|
|
2093
|
+
value simply means no \`authorization\` header is sent. Every Seneca
|
|
2094
|
+
provider is configured the same way, so an application that later moves
|
|
2095
|
+
to an authenticated service changes one value rather than its shape.
|
|
2096
|
+
And \`get:info\` is answered by the plugin itself, without calling the
|
|
2097
|
+
API, so a reply tells you the plugin loaded and initialised before any
|
|
2098
|
+
request goes anywhere.
|
|
2099
|
+
|
|
2100
|
+
`)
|
|
2101
|
+
|
|
2102
|
+
if (subject.cmds.includes('list')) {
|
|
2103
|
+
Content(`${step(`List the ${subject.name} records`)}
|
|
2104
|
+
|
|
2105
|
+
${'list' === noteAt ? nestedNote : ''}Replace the \`console.log(info)\` line with:
|
|
2106
|
+
|
|
2107
|
+
\`\`\`js
|
|
2108
|
+
${subjPre} const ${subjVar} = await seneca
|
|
2109
|
+
.entity('${canon(subject.name)}')
|
|
2110
|
+
.list$(${listArg})
|
|
2111
|
+
|
|
2112
|
+
console.log('Found ' + ${subjVar}.length + ' ${subject.name} record(s):')
|
|
2113
|
+
${subjVar}.forEach((r) => {
|
|
2114
|
+
console.log(' ' + r.${idf}${shown.map((f: any) => ` + ' ' + r.${f.name}`).join('')})
|
|
2115
|
+
})
|
|
2116
|
+
\`\`\`
|
|
2117
|
+
|
|
2118
|
+
`)
|
|
2119
|
+
Content(offline ?
|
|
2120
|
+
`Run it again and you will see the two ${subject.name}
|
|
2121
|
+
records you seeded, under the ids they are filed by.
|
|
2122
|
+
|
|
2123
|
+
` :
|
|
2124
|
+
`Run it again and you will see every ${subject.name}
|
|
2125
|
+
record the server holds.
|
|
2126
|
+
|
|
2127
|
+
`)
|
|
2128
|
+
Content(`No URL, no HTTP verb, no JSON parsing. You asked a Seneca entity for
|
|
2129
|
+
a list, the provider turned that into an SDK call, and the SDK turned
|
|
2130
|
+
it into a request. These are ordinary Seneca entities, so everything
|
|
2131
|
+
you already know about the entity API applies to them.
|
|
2132
|
+
|
|
2133
|
+
`)
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
if (subject.cmds.includes('load')) {
|
|
2137
|
+
Content(`${step(`Load one ${subject.name}`)}
|
|
2138
|
+
|
|
2139
|
+
${'load' === noteAt ? nestedNote : ''}Add:
|
|
2140
|
+
|
|
2141
|
+
\`\`\`js
|
|
2142
|
+
const one = await seneca
|
|
2143
|
+
.entity('${canon(subject.name)}')
|
|
2144
|
+
.load$(${oneArg(firstId)})
|
|
2145
|
+
|
|
2146
|
+
console.log('loaded', one.${idf}${0 < shown.length ? `, one.${shown[0].name}` : ''})
|
|
2147
|
+
\`\`\`
|
|
2148
|
+
|
|
2149
|
+
`)
|
|
2150
|
+
Content(subject.cmds.includes('list') ?
|
|
2151
|
+
`\`list$\` gives you many, \`load$\` gives you one. Now ask for
|
|
2152
|
+
something that is not there:
|
|
2153
|
+
|
|
2154
|
+
` :
|
|
2155
|
+
`\`load$\` gives you one record by its id. Now ask for something
|
|
2156
|
+
that is not there:
|
|
2157
|
+
|
|
2158
|
+
`)
|
|
2159
|
+
Content(`\`\`\`js
|
|
2160
|
+
const missing = await seneca
|
|
2161
|
+
.entity('${canon(subject.name)}')
|
|
2162
|
+
.load$(${oneArg(`'nosuch${subject.name}'`)})
|
|
2163
|
+
|
|
2164
|
+
console.log('missing =', missing) // null
|
|
2165
|
+
\`\`\`
|
|
2166
|
+
|
|
2167
|
+
You get \`null\`, not an exception. "There is no such
|
|
2168
|
+
${subject.name}" is an ordinary answer to a lookup, so it does not
|
|
2169
|
+
interrupt your code.
|
|
2170
|
+
|
|
2171
|
+
`)
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
if (canWrite) {
|
|
2175
|
+
Content(`${step('Create, change and remove')}
|
|
2176
|
+
|
|
2177
|
+
${'write' === noteAt ? nestedNote : ''}`)
|
|
2178
|
+
Content(hasRead ?
|
|
2179
|
+
`Everything so far has been reading. This entity accepts writes too,
|
|
2180
|
+
so add:
|
|
2181
|
+
|
|
2182
|
+
` :
|
|
2183
|
+
`Now write one. Add:
|
|
2184
|
+
|
|
2185
|
+
`)
|
|
2186
|
+
Content(`\`\`\`js
|
|
2187
|
+
// Create: make$ builds an entity, save$ persists it.
|
|
2188
|
+
let ${subjOne} = await seneca
|
|
2189
|
+
.entity('${canon(subject.name)}')
|
|
2190
|
+
.make$({ ${makeFields.join(', ')} })
|
|
2191
|
+
.save$()
|
|
2192
|
+
|
|
2193
|
+
console.log('created with id', ${subjOne}.${idf})
|
|
2194
|
+
\`\`\`
|
|
2195
|
+
|
|
2196
|
+
Run it, and note the id printed. It is **not** one you chose — the
|
|
2197
|
+
${source} assigns ids itself and ignores any you send. That is worth
|
|
2198
|
+
knowing before you write code that assumes otherwise.
|
|
2199
|
+
|
|
2200
|
+
`)
|
|
2201
|
+
|
|
2202
|
+
if (canUpdate) {
|
|
2203
|
+
Content(`Now change it. An entity that already carries an id is an update
|
|
2204
|
+
rather than a create, and \`save$\` decides between the two on exactly
|
|
2205
|
+
that:
|
|
2206
|
+
|
|
2207
|
+
\`\`\`js
|
|
2208
|
+
${subjOne}.${plainFields[0].name} = ${litval(plainFields[0], true)}
|
|
2209
|
+
${subjOne} = await ${subjOne}.save$()
|
|
2210
|
+
|
|
2211
|
+
console.log('updated:', ${subjOne}.${plainFields[0].name})
|
|
2212
|
+
\`\`\`
|
|
2213
|
+
|
|
2214
|
+
`)
|
|
2215
|
+
}
|
|
2216
|
+
|
|
2217
|
+
if (canRemove) {
|
|
2218
|
+
Content(`And remove it, leaving the ${source} as you found it:
|
|
2219
|
+
|
|
2220
|
+
\`\`\`js
|
|
2221
|
+
await seneca
|
|
2222
|
+
.entity('${canon(subject.name)}')
|
|
2223
|
+
.remove$(${oneArg(`${subjOne}.${idf}`)})
|
|
2224
|
+
\`\`\`
|
|
2225
|
+
|
|
2226
|
+
`)
|
|
2227
|
+
if (subject.cmds.includes('load')) {
|
|
2228
|
+
Content(`Load it once more and, as before, you get \`null\`:
|
|
2229
|
+
|
|
2230
|
+
\`\`\`js
|
|
2231
|
+
console.log(
|
|
2232
|
+
'after remove:',
|
|
2233
|
+
await seneca
|
|
2234
|
+
.entity('${canon(subject.name)}')
|
|
2235
|
+
.load$(${oneArg(`${subjOne}.${idf}`)})
|
|
2236
|
+
) // null
|
|
2237
|
+
\`\`\`
|
|
2238
|
+
|
|
2239
|
+
`)
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
else {
|
|
2243
|
+
Content(`This entity declares no remove operation, so the record you have just
|
|
2244
|
+
created stays where it is.
|
|
2245
|
+
|
|
2246
|
+
`)
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
Content(`Those are the only methods there are:
|
|
2250
|
+
|
|
2251
|
+
${cmdList}
|
|
2252
|
+
|
|
2253
|
+
They behave the same way on every entity this plugin exposes.
|
|
2254
|
+
|
|
2255
|
+
`)
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
if (null != child) {
|
|
2259
|
+
const ckey = child.parents[0]
|
|
2260
|
+
const cidf = child.idf || 'id'
|
|
2261
|
+
const cparent = null == childParent ? 'their parent' :
|
|
2262
|
+
`${childParent.name} records`
|
|
2263
|
+
const cop = child.cmds.includes('list') ? 'list' : 'load'
|
|
2264
|
+
|
|
2265
|
+
// Live, the parent id comes from a list; offline it is seeded, so a
|
|
2266
|
+
// literal is both shorter and exactly reproducible.
|
|
2267
|
+
const reuse = `${subjVar}[0].${idf}`
|
|
2268
|
+
const cval = offline ? `'${seedParentVal(child, ckey)}'` :
|
|
2269
|
+
(null != childParent && childParent.name === subject.name &&
|
|
2270
|
+
subject.cmds.includes('list') && 0 === subject.parents.length) ?
|
|
2271
|
+
reuse : ident(ckey)
|
|
2272
|
+
|
|
2273
|
+
const cpre = (live && reuse !== cval) ?
|
|
2274
|
+
` const ${plural(childParent.name)} = await seneca
|
|
2275
|
+
.entity('${canon(childParent.name)}')
|
|
2276
|
+
.list$()
|
|
2277
|
+
const ${ident(ckey)} = ${plural(childParent.name)}[0].${childParent.idf || 'id'}
|
|
2278
|
+
|
|
2279
|
+
` : ''
|
|
2280
|
+
|
|
2281
|
+
const cargs = child.parents.map((k: string) => k === ckey ?
|
|
2282
|
+
`${qkey(k)}: ${cval}` : `${qkey(k)}: '${seedParentVal(child, k)}'`)
|
|
2283
|
+
|
|
2284
|
+
Content(`${step(`Reach the ${child.name} records`)}
|
|
2285
|
+
|
|
2286
|
+
${cap(child.name)} records live inside ${cparent}, and the API route
|
|
2287
|
+
says so:
|
|
2288
|
+
|
|
2289
|
+
\`${child.path}\`
|
|
2290
|
+
|
|
2291
|
+
The parent id in that path is not optional, so every ${child.name}
|
|
2292
|
+
call needs a \`${ckey}\` in its query:
|
|
2293
|
+
|
|
2294
|
+
\`\`\`js
|
|
2295
|
+
`)
|
|
2296
|
+
|
|
2297
|
+
if (child.cmds.includes('list')) {
|
|
2298
|
+
Content(`${cpre} const ${plural(child.name)} = await seneca
|
|
2299
|
+
.entity('${canon(child.name)}')
|
|
2300
|
+
.list$({ ${cargs.join(', ')} })
|
|
2301
|
+
|
|
2302
|
+
console.log('found ' + ${plural(child.name)}.length + ' ${child.name} record(s)')
|
|
2303
|
+
`)
|
|
2304
|
+
}
|
|
2305
|
+
else {
|
|
2306
|
+
Content(`${cpre} const found = await seneca
|
|
2307
|
+
.entity('${canon(child.name)}')
|
|
2308
|
+
.load$({ ${cargs.concat([`${qkey(cidf)}: '${child.name}0'`]).join(', ')} })
|
|
2309
|
+
|
|
2310
|
+
console.log('found', found.${cidf})
|
|
2311
|
+
`)
|
|
2312
|
+
}
|
|
2313
|
+
|
|
2314
|
+
Content(`\`\`\`
|
|
2315
|
+
|
|
2316
|
+
Leave the \`${ckey}\` out and the call throws at once, naming the key it
|
|
2317
|
+
needed, rather than letting a half-built URL come back as a puzzling
|
|
2318
|
+
404:
|
|
2319
|
+
|
|
2320
|
+
\`\`\`js
|
|
2321
|
+
// throws: ${provider.pkgName}: ${child.name} ${cop}: ${ckey} is required
|
|
2322
|
+
await seneca
|
|
2323
|
+
.entity('${canon(child.name)}')
|
|
2324
|
+
.${'list' === cop ? 'list$()' : `load$('${child.name}0')`}
|
|
2325
|
+
\`\`\`
|
|
2326
|
+
|
|
2327
|
+
`)
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
if (offline) {
|
|
2331
|
+
Content(`## Talking to a real server
|
|
2332
|
+
|
|
2333
|
+
The script you have just written never touched the network. To point it
|
|
2334
|
+
at a running ${provider.api} server instead, replace the \`test\` and
|
|
2335
|
+
\`testopts\` options with that server's base URL:
|
|
2336
|
+
|
|
2337
|
+
\`\`\`js
|
|
2338
|
+
.use('${provider.pkgName}', {
|
|
2339
|
+
sdk: { base: '${hasServer ? provider.liveBase : 'https://api.example.com'}' },
|
|
2340
|
+
})
|
|
2341
|
+
\`\`\`
|
|
2342
|
+
|
|
2343
|
+
Nothing else in the script changes — the entity calls are the same
|
|
2344
|
+
calls. Your seeded ids will not exist there, so read the ids you need
|
|
2345
|
+
from a \`list$\` first.
|
|
2346
|
+
|
|
2347
|
+
`)
|
|
2348
|
+
if (hasServer && '' !== sdkRepo) {
|
|
2349
|
+
Content(`A test server that answers on that address is distributed in the SDK's
|
|
2350
|
+
source repository, which is the only place it ships. Clone
|
|
2351
|
+
\`${sdkRepo}\`, then run \`npm install\`, \`npm run build\` and
|
|
2352
|
+
\`npm start\` in its \`app\` folder.
|
|
2353
|
+
|
|
2354
|
+
`)
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
Content(`## What you have learned
|
|
2359
|
+
|
|
2360
|
+
You built a script that ${canWrite ? 'reads and writes' : 'reads'}
|
|
2361
|
+
${provider.api} data through Seneca entities,
|
|
2362
|
+
${live ? 'against a real server' : 'with no server involved'}. Along
|
|
2363
|
+
the way you saw:
|
|
2364
|
+
|
|
2365
|
+
- Provider configuration has the same shape even when no credentials
|
|
2366
|
+
are needed.
|
|
2367
|
+
- API resources are Seneca entities under \`provider/${provider.lower}/\`,
|
|
2368
|
+
reached with the entity API you already know.
|
|
2369
|
+
`)
|
|
2370
|
+
if (null != child || 0 < subject.parents.length) {
|
|
2371
|
+
Content(`- A resource nested under another in the API needs its parent's id in
|
|
2372
|
+
every query, and says which key is missing when you forget.
|
|
2373
|
+
`)
|
|
2374
|
+
}
|
|
2375
|
+
if (subject.cmds.includes('load')) {
|
|
2376
|
+
Content(`- \`load$\` answers \`null\` for something that is not there, rather
|
|
2377
|
+
than throwing.
|
|
2378
|
+
`)
|
|
2379
|
+
}
|
|
2380
|
+
if (canWrite) {
|
|
2381
|
+
Content(`- \`save$\` creates without an id and updates with one, and the
|
|
2382
|
+
${source} chooses the id.
|
|
2383
|
+
`)
|
|
2384
|
+
}
|
|
2385
|
+
if (offline) {
|
|
2386
|
+
Content(`- The offline store makes all of this runnable with nothing installed
|
|
2387
|
+
but npm packages, which is also how you test your own code.
|
|
2388
|
+
`)
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
Content(`
|
|
2392
|
+
## Where to go next
|
|
2393
|
+
|
|
2394
|
+
- To do a specific job — ${live ? 'run without a server' : 'point at a real server'}, reach the raw SDK,
|
|
2395
|
+
test your own code — see the [how-to guides](how-to.md).
|
|
2396
|
+
- To look up an exact pattern, field or option, see the
|
|
2397
|
+
[reference](reference.md).
|
|
2398
|
+
- To understand why the plugin is built this way — why entities rather
|
|
2399
|
+
than one message per route, and what it does with the SDK's answers
|
|
2400
|
+
— see the [explanation](explanation.md).
|
|
2401
|
+
- For what each of these documents is for, see the
|
|
2402
|
+
[documentation index](README.md).
|
|
2403
|
+
`)
|
|
2404
|
+
})
|
|
2405
|
+
})
|
|
2406
|
+
|
|
2407
|
+
|
|
2408
|
+
// --- doc/how-to.md ----------------------------------------------------------
|
|
2409
|
+
//
|
|
2410
|
+
// The task-oriented quadrant of the Diataxis set: one problem per section, for
|
|
2411
|
+
// a reader who already has the plugin loaded. It instructs and does not
|
|
2412
|
+
// explain — anything that starts justifying a design choice belongs in
|
|
2413
|
+
// explanation.md and is linked to instead.
|
|
2414
|
+
//
|
|
2415
|
+
// Two decisions worth naming. First, the section list is built as data before
|
|
2416
|
+
// anything is emitted, so the table of contents and the sections themselves
|
|
2417
|
+
// are produced from the SAME guards and cannot drift: a recipe that is
|
|
2418
|
+
// suppressed because no entity declares the cmd also loses its TOC entry.
|
|
2419
|
+
// Second, every example id is the one `seedRecord` gives that entity, so the
|
|
2420
|
+
// examples here and the seed in test/seed.js agree — the offline recipe can
|
|
2421
|
+
// then be copied verbatim and the ids used in every other recipe will
|
|
2422
|
+
// actually resolve.
|
|
2423
|
+
|
|
2424
|
+
const DocHowto = cmp(function DocHowto(props: any) {
|
|
2425
|
+
const { provider } = props
|
|
2426
|
+
|
|
2427
|
+
const ents = provider.entities
|
|
2428
|
+
const nested = ents.filter((e: any) => 0 < e.parents.length)
|
|
2429
|
+
|
|
2430
|
+
// The model gives '' when the API definition declares no server. Normalise
|
|
2431
|
+
// an absent value to the same thing, so a missing base is treated as absent
|
|
2432
|
+
// rather than printed as a default nobody can use.
|
|
2433
|
+
const liveBase = provider.liveBase || ''
|
|
2434
|
+
|
|
2435
|
+
// The same choice the tests and the manual scripts make: fewest parent keys
|
|
2436
|
+
// (nothing to arrange), then most cmds. Recipes prefer it, so one entity
|
|
2437
|
+
// carries the reader through the document wherever it can.
|
|
2438
|
+
const subject = [...ents]
|
|
2439
|
+
.sort((a: any, b: any) =>
|
|
2440
|
+
(a.parents.length - b.parents.length) || (b.cmds.length - a.cmds.length))[0]
|
|
2441
|
+
|
|
2442
|
+
const forCmd = (cmd: string) => {
|
|
2443
|
+
const able = ents.filter((e: any) => e.cmds.includes(cmd))
|
|
2444
|
+
return able.find((e: any) => e === subject) ||
|
|
2445
|
+
able.find((e: any) => 0 === e.parents.length) ||
|
|
2446
|
+
able[0] || null
|
|
2447
|
+
}
|
|
2448
|
+
|
|
2449
|
+
const canon = (e: any) => `provider/${provider.lower}/${e.name}`
|
|
2450
|
+
const idf = (e: any) => e.idf || 'id'
|
|
2451
|
+
|
|
2452
|
+
// A parent key's example value. This MIRRORS seedRecord rather than
|
|
2453
|
+
// inventing something more readable: the offline recipe below seeds with
|
|
2454
|
+
// seedRecord, and an example id that does not match what was seeded turns
|
|
2455
|
+
// every other recipe into a lookup that answers null.
|
|
2456
|
+
const parentVal = (e: any, k: string) => {
|
|
2457
|
+
const f = e.fields.find((f: any) => f.name === k)
|
|
2458
|
+
return null == f ? `${k.replace(/_id$/, '')}0` : `${f.parentEntity}0`
|
|
2459
|
+
}
|
|
2460
|
+
|
|
2461
|
+
const parentArgs = (e: any) =>
|
|
2462
|
+
e.parents.map((k: string) => `${k}: '${parentVal(e, k)}'`).join(', ')
|
|
2463
|
+
|
|
2464
|
+
// A query naming ONE record. A top-level entity takes the bare id string;
|
|
2465
|
+
// a nested one cannot, because it is identified by the whole set of keys.
|
|
2466
|
+
const oneArgs = (e: any) => 0 === e.parents.length ?
|
|
2467
|
+
`'${e.name}0'` : `{ ${parentArgs(e)}, ${idf(e)}: '${e.name}0' }`
|
|
2468
|
+
|
|
2469
|
+
const listArgs = (e: any) =>
|
|
2470
|
+
0 === e.parents.length ? '' : `{ ${parentArgs(e)} }`
|
|
2471
|
+
|
|
2472
|
+
// The SDK's own entity ops always take an object, even for a bare id.
|
|
2473
|
+
const sdkLoadArgs = (e: any) => 0 === e.parents.length ?
|
|
2474
|
+
`{ ${idf(e)}: '${e.name}0' }` :
|
|
2475
|
+
`{ ${parentArgs(e)}, ${idf(e)}: '${e.name}0' }`
|
|
2476
|
+
|
|
2477
|
+
const key = (k: string) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(k) ? k : `'${k}'`
|
|
2478
|
+
|
|
2479
|
+
const literal = (rec: Record<string, any>) => {
|
|
2480
|
+
const names = Object.keys(rec)
|
|
2481
|
+
if (0 === names.length) {
|
|
2482
|
+
return '{}'
|
|
2483
|
+
}
|
|
2484
|
+
return '{ ' + names
|
|
2485
|
+
.map((k) => `${key(k)}: ` +
|
|
2486
|
+
('string' === typeof rec[k] ? `'${rec[k]}'` : String(rec[k])))
|
|
2487
|
+
.join(', ') + ' }'
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
// What a create sends: the seeded record without its id, because the id is
|
|
2491
|
+
// the API's to assign. Parent keys stay — a nested write carries them in
|
|
2492
|
+
// the data rather than the query.
|
|
2493
|
+
const createData = (e: any) => {
|
|
2494
|
+
const rec = seedRecord(e, 0)
|
|
2495
|
+
delete rec[idf(e)]
|
|
2496
|
+
delete rec.id
|
|
2497
|
+
return rec
|
|
2498
|
+
}
|
|
2499
|
+
|
|
2500
|
+
const changeable = (e: any) => e.fields.find((f: any) =>
|
|
2501
|
+
f.name !== idf(e) && 'id' !== f.name && !e.parents.includes(f.name))
|
|
2502
|
+
|
|
2503
|
+
const newValue = (f: any) => 'number' === f.kind ? '999' :
|
|
2504
|
+
'boolean' === f.kind ? 'true' : `'${f.name}-changed'`
|
|
2505
|
+
|
|
2506
|
+
const pathParams = (p: string) =>
|
|
2507
|
+
(String(p).match(/\{([^}]+)\}/g) || []).map((s: string) => s.slice(1, -1))
|
|
2508
|
+
|
|
2509
|
+
const eList = forCmd('list')
|
|
2510
|
+
const eLoad = forCmd('load')
|
|
2511
|
+
const eSave = forCmd('save')
|
|
2512
|
+
const eRemove = forCmd('remove')
|
|
2513
|
+
|
|
2514
|
+
// Sections as data, so the contents list and the sections cannot disagree.
|
|
2515
|
+
const sections: any[] = []
|
|
2516
|
+
const sec = (title: string, body: string) => sections.push({ title, body })
|
|
2517
|
+
|
|
2518
|
+
// GitHub's heading anchors: lowercased, punctuation dropped, spaces
|
|
2519
|
+
// hyphenated. Section titles avoid backticks and full stops so this stays
|
|
2520
|
+
// a faithful reproduction rather than an approximation.
|
|
2521
|
+
const anchor = (title: string) => '#' + title.toLowerCase()
|
|
2522
|
+
.replace(/[^a-z0-9 _-]/g, '').trim().replace(/ +/g, '-')
|
|
2523
|
+
|
|
2524
|
+
const NESTED_TITLE = 'Work with nested entities'
|
|
2525
|
+
const OFFLINE_TITLE = 'Run offline, without a server'
|
|
2526
|
+
|
|
2527
|
+
|
|
2528
|
+
if (null != eList) {
|
|
2529
|
+
sec('List the records of an entity', `Every resource this plugin covers is a Seneca entity under
|
|
2530
|
+
\`provider/${provider.lower}/\`, so listing one is \`list$\`:
|
|
2531
|
+
|
|
2532
|
+
\`\`\`js
|
|
2533
|
+
const ${eList.name}s = await seneca
|
|
2534
|
+
.entity('${canon(eList)}')
|
|
2535
|
+
.list$(${listArgs(eList)})
|
|
2536
|
+
\`\`\`
|
|
2537
|
+
|
|
2538
|
+
You get an ordinary array of Seneca entities back, so \`length\`, \`map\`
|
|
2539
|
+
and \`data$()\` behave exactly as they do for any other store.
|
|
2540
|
+
|
|
2541
|
+
Fields in the query travel to the API as match criteria. Seneca's own
|
|
2542
|
+
directives — \`sort$\`, \`limit$\` and the rest — are stripped before the
|
|
2543
|
+
call, because they are features of a database store and not of an HTTP
|
|
2544
|
+
API. If you need ordering or paging, ask the API for it using fields it
|
|
2545
|
+
recognises, or sort the returned array yourself.${0 < nested.length ? `
|
|
2546
|
+
|
|
2547
|
+
An entity nested under a parent in the API path cannot be listed without
|
|
2548
|
+
the parent's id; see [${NESTED_TITLE}](${anchor(NESTED_TITLE)}).` : ''}`)
|
|
2549
|
+
}
|
|
2550
|
+
|
|
2551
|
+
|
|
2552
|
+
if (null != eLoad) {
|
|
2553
|
+
sec('Read one record by id', `\`load$\` answers a single record:
|
|
2554
|
+
|
|
2555
|
+
\`\`\`js
|
|
2556
|
+
const ${eLoad.name} = await seneca
|
|
2557
|
+
.entity('${canon(eLoad)}')
|
|
2558
|
+
.load$(${oneArgs(eLoad)})
|
|
2559
|
+
\`\`\`
|
|
2560
|
+
${'id' === idf(eLoad) ? '' : `
|
|
2561
|
+
The id field for \`${eLoad.name}\` is \`${idf(eLoad)}\`, so that is the
|
|
2562
|
+
key to supply.
|
|
2563
|
+
`}
|
|
2564
|
+
A record that is not there comes back as \`null\`. It is not an error and
|
|
2565
|
+
it does not throw, so test the value rather than wrapping the call:
|
|
2566
|
+
|
|
2567
|
+
\`\`\`js
|
|
2568
|
+
const missing = await seneca
|
|
2569
|
+
.entity('${canon(eLoad)}')
|
|
2570
|
+
.load$(${0 === eLoad.parents.length ? `'nosuch'` :
|
|
2571
|
+
`{ ${parentArgs(eLoad)}, ${idf(eLoad)}: 'nosuch' }`})
|
|
2572
|
+
|
|
2573
|
+
if (null == missing) {
|
|
2574
|
+
// no such ${eLoad.name}
|
|
2575
|
+
}
|
|
2576
|
+
\`\`\`
|
|
2577
|
+
|
|
2578
|
+
Everything else that can go wrong — a network failure, a 5xx, a rejected
|
|
2579
|
+
key — does throw, so an unhandled rejection still means something is
|
|
2580
|
+
genuinely wrong.`)
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
|
|
2584
|
+
if (null != eSave) {
|
|
2585
|
+
const created = literal(createData(eSave))
|
|
2586
|
+
|
|
2587
|
+
sec('Create a record', `\`make$\` builds an entity and \`save$\` writes it. An entity with no id
|
|
2588
|
+
is a create:
|
|
2589
|
+
|
|
2590
|
+
\`\`\`js
|
|
2591
|
+
const ${eSave.name} = await seneca
|
|
2592
|
+
.entity('${canon(eSave)}')
|
|
2593
|
+
.make$(${created})
|
|
2594
|
+
.save$()
|
|
2595
|
+
|
|
2596
|
+
console.log(${eSave.name}.${idf(eSave)})
|
|
2597
|
+
\`\`\`
|
|
2598
|
+
${0 === eSave.parents.length ? '' : `
|
|
2599
|
+
Note that \`${eSave.parents.join('`, `')}\` travels in the DATA for a write,
|
|
2600
|
+
not in a query: a \`${eSave.name}\` is created inside its parent.
|
|
2601
|
+
`}
|
|
2602
|
+
\`save$\` resolves to the record as the API returned it, which is the only
|
|
2603
|
+
reliable source of the id. Read it from there rather than predicting it:
|
|
2604
|
+
what an API does with an id you supply on create is its own business, and
|
|
2605
|
+
several ignore it entirely.`)
|
|
2606
|
+
|
|
2607
|
+
const f = changeable(eSave)
|
|
2608
|
+
|
|
2609
|
+
sec('Update a record', `The same call updates. \`save$\` dispatches on the id: an entity carrying
|
|
2610
|
+
one is an update, an entity without one is a create. So the safe shape is
|
|
2611
|
+
load, change, save:
|
|
2612
|
+
|
|
2613
|
+
\`\`\`js${eSave.cmds.includes('load') ? `
|
|
2614
|
+
const ${eSave.name} = await seneca
|
|
2615
|
+
.entity('${canon(eSave)}')
|
|
2616
|
+
.load$(${oneArgs(eSave)})
|
|
2617
|
+
` : `
|
|
2618
|
+
const ${eSave.name} = seneca
|
|
2619
|
+
.entity('${canon(eSave)}')
|
|
2620
|
+
.make$(${literal(0 === eSave.parents.length ?
|
|
2621
|
+
{ [idf(eSave)]: `${eSave.name}0` } :
|
|
2622
|
+
{ ...Object.fromEntries(eSave.parents.map(
|
|
2623
|
+
(k: string) => [k, parentVal(eSave, k)])),
|
|
2624
|
+
[idf(eSave)]: `${eSave.name}0` })})
|
|
2625
|
+
`}${null == f ? `
|
|
2626
|
+
// change the fields you need
|
|
2627
|
+
` : `
|
|
2628
|
+
${eSave.name}.${f.name} = ${newValue(f)}
|
|
2629
|
+
`}
|
|
2630
|
+
await ${eSave.name}.save$()
|
|
2631
|
+
\`\`\`
|
|
2632
|
+
|
|
2633
|
+
Mutating the record you loaded sends it as it stood plus your change, so
|
|
2634
|
+
you do not depend on how the API treats a request that omits fields —
|
|
2635
|
+
some merge, some replace.`)
|
|
2636
|
+
}
|
|
2637
|
+
|
|
2638
|
+
|
|
2639
|
+
if (null != eRemove) {
|
|
2640
|
+
sec('Remove a record', `\`\`\`js
|
|
2641
|
+
await seneca
|
|
2642
|
+
.entity('${canon(eRemove)}')
|
|
2643
|
+
.remove$(${oneArgs(eRemove)})
|
|
2644
|
+
\`\`\`
|
|
2645
|
+
${0 === eRemove.parents.length ? '' : `
|
|
2646
|
+
As with a read, the parent keys are part of naming the record, so they go
|
|
2647
|
+
in the query object alongside the id.
|
|
2648
|
+
`}${eRemove.cmds.includes('load') ? `
|
|
2649
|
+
A \`load$\` of the same id afterwards answers \`null\`.` :
|
|
2650
|
+
`
|
|
2651
|
+
\`remove$\` resolves once the API has accepted the removal.`}`)
|
|
2652
|
+
}
|
|
2653
|
+
|
|
2654
|
+
|
|
2655
|
+
if (0 < nested.length) {
|
|
2656
|
+
// A nested entity that declares no cmds has nothing to demonstrate, so
|
|
2657
|
+
// prefer one that does; the error example needs a command that exists.
|
|
2658
|
+
const n = nested.find((e: any) => 0 < e.cmds.length) || nested[0]
|
|
2659
|
+
const firstCmd = n.cmds[0] || 'list'
|
|
2660
|
+
|
|
2661
|
+
sec(NESTED_TITLE, `Some resources live inside a parent, and the API path says so — the
|
|
2662
|
+
route for \`${n.name}\` is:
|
|
2663
|
+
|
|
2664
|
+
\`\`\`
|
|
2665
|
+
${n.path}
|
|
2666
|
+
\`\`\`
|
|
2667
|
+
|
|
2668
|
+
So a \`${n.name}\` cannot be addressed at all without its parent's id, and
|
|
2669
|
+
the provider requires those keys on every command.
|
|
2670
|
+
|
|
2671
|
+
${nested.map((e: any) =>
|
|
2672
|
+
`- \`${e.name}\` requires \`${e.parents.join('`, `')}\``).join('\n')}
|
|
2673
|
+
|
|
2674
|
+
For reads the keys go in the query; for writes they go in the data:
|
|
2675
|
+
|
|
2676
|
+
\`\`\`js${n.cmds.includes('list') ? `
|
|
2677
|
+
await seneca.entity('${canon(n)}').list$({ ${parentArgs(n)} })
|
|
2678
|
+
` : ''}${n.cmds.includes('load') ? `
|
|
2679
|
+
await seneca.entity('${canon(n)}')
|
|
2680
|
+
.load$({ ${parentArgs(n)}, ${idf(n)}: '${n.name}0' })
|
|
2681
|
+
` : ''}${n.cmds.includes('save') ? `
|
|
2682
|
+
await seneca.entity('${canon(n)}')
|
|
2683
|
+
.make$(${literal(createData(n))})
|
|
2684
|
+
.save$()
|
|
2685
|
+
` : ''}${n.cmds.includes('remove') ? `
|
|
2686
|
+
await seneca.entity('${canon(n)}')
|
|
2687
|
+
.remove$({ ${parentArgs(n)}, ${idf(n)}: '${n.name}0' })
|
|
2688
|
+
` : ''}\`\`\`
|
|
2689
|
+
|
|
2690
|
+
Leave a key out and the call throws at once, naming what is missing:
|
|
2691
|
+
|
|
2692
|
+
\`\`\`
|
|
2693
|
+
${provider.pkgName}: ${n.name} ${firstCmd}: ${n.parents[0]} is required
|
|
2694
|
+
\`\`\`
|
|
2695
|
+
|
|
2696
|
+
That is deliberate: without it the SDK would build half a URL and the
|
|
2697
|
+
server would answer 404, which is a much harder message to act on. The
|
|
2698
|
+
[explanation](explanation.md) covers why this is a guard rather than a
|
|
2699
|
+
silent default.`)
|
|
2700
|
+
}
|
|
2701
|
+
|
|
2702
|
+
|
|
2703
|
+
{
|
|
2704
|
+
// Seed the entity the recipes use, plus the first nested entity AND its
|
|
2705
|
+
// parent — a child seeded under a parent that is not there lists as
|
|
2706
|
+
// empty, which reads as a passing test that proves nothing.
|
|
2707
|
+
const seeded: any[] = []
|
|
2708
|
+
const add = (e: any) => {
|
|
2709
|
+
if (null != e && !seeded.includes(e)) {
|
|
2710
|
+
seeded.push(e)
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
const child = 0 < nested.length ? nested[0] : null
|
|
2714
|
+
const parent = null == child ? null :
|
|
2715
|
+
ents.find((p: any) => p.name === child.parentEntity)
|
|
2716
|
+
|
|
2717
|
+
add(subject)
|
|
2718
|
+
if (null != child) {
|
|
2719
|
+
add(parent)
|
|
2720
|
+
add(child)
|
|
2721
|
+
}
|
|
2722
|
+
seeded.sort((a: any, b: any) => ents.indexOf(a) - ents.indexOf(b))
|
|
2723
|
+
|
|
2724
|
+
const seed = seeded.map((e: any) => ` ${e.name}: {
|
|
2725
|
+
` + [0, 1].map((i: number) =>
|
|
2726
|
+
` ${e.name}${i}: ${literal(seedRecord(e, i))},`).join('\n') + `
|
|
2727
|
+
},`).join('\n')
|
|
2728
|
+
|
|
2729
|
+
sec(OFFLINE_TITLE, `The SDK ships an in-memory mock transport. Turn it on with \`test\` and
|
|
2730
|
+
seed it with \`testopts\`:
|
|
2731
|
+
|
|
2732
|
+
\`\`\`js
|
|
2733
|
+
.use('${provider.pkgName}', {
|
|
2734
|
+
test: true,
|
|
2735
|
+
testopts: {
|
|
2736
|
+
entity: {
|
|
2737
|
+
${seed}
|
|
2738
|
+
},
|
|
2739
|
+
},
|
|
2740
|
+
})
|
|
2741
|
+
\`\`\`
|
|
2742
|
+
|
|
2743
|
+
Records are keyed by id under their entity name, and the id inside the
|
|
2744
|
+
record has to match the key it is filed under. Every command then works
|
|
2745
|
+
offline, not-found included: an id you did not seed answers \`null\`,
|
|
2746
|
+
exactly as it would against a real server.${null == parent || null == child ? '' : `
|
|
2747
|
+
|
|
2748
|
+
A nested record has to point at a parent that is actually seeded: each
|
|
2749
|
+
\`${child.name}\` above carries \`${child.parents[0]}: '${parentVal(child, child.parents[0])}'\`, and
|
|
2750
|
+
that is a \`${parent.name}\` the seed contains. Seed a child under a parent
|
|
2751
|
+
that is not there and its list comes back empty rather than failing —
|
|
2752
|
+
which, in a test, reads as a pass that proves nothing.`}
|
|
2753
|
+
|
|
2754
|
+
This is how this plugin's own suite runs, and it is the recommended way
|
|
2755
|
+
to test application code that uses the provider: no server, no network,
|
|
2756
|
+
and the same code path as production. See \`test/seed.js\`, which seeds
|
|
2757
|
+
every entity this way.`)
|
|
2758
|
+
}
|
|
2759
|
+
|
|
2760
|
+
|
|
2761
|
+
sec('Point at a different server', `The \`sdk\` option is passed straight to the \`${provider.sdkClass}\`
|
|
2762
|
+
constructor, so \`base\` chooses the host:
|
|
2763
|
+
|
|
2764
|
+
\`\`\`js
|
|
2765
|
+
.use('${provider.pkgName}', {
|
|
2766
|
+
sdk: { base: 'https://${provider.lower}.example.com' },
|
|
2767
|
+
})
|
|
2768
|
+
\`\`\`
|
|
2769
|
+
|
|
2770
|
+
${'' === liveBase ?
|
|
2771
|
+
`The API definition declares no server, so there is no default worth
|
|
2772
|
+
relying on: set \`base\` explicitly, or run against the mock instead (see
|
|
2773
|
+
[${OFFLINE_TITLE}](${anchor(OFFLINE_TITLE)})).` :
|
|
2774
|
+
`The SDK's own default is \`${liveBase}\`, which is where the
|
|
2775
|
+
companion test server listens, so local development usually needs no
|
|
2776
|
+
\`base\` at all.`}`)
|
|
2777
|
+
|
|
2778
|
+
|
|
2779
|
+
sec('Send an API key', `Credentials are not a plugin option: they come through the provider
|
|
2780
|
+
convention, so that every provider in an application is configured the
|
|
2781
|
+
same way. Declare the variable with \`env\` and set the key under this
|
|
2782
|
+
provider's name:
|
|
2783
|
+
|
|
2784
|
+
\`\`\`js
|
|
2785
|
+
.use('env', {
|
|
2786
|
+
var: { $${provider.ENV}_APIKEY: String },
|
|
2787
|
+
})
|
|
2788
|
+
.use('provider', {
|
|
2789
|
+
provider: {
|
|
2790
|
+
${provider.lower}: {
|
|
2791
|
+
keys: {
|
|
2792
|
+
apikey: { value: '$${provider.ENV}_APIKEY' },
|
|
2793
|
+
},
|
|
2794
|
+
},
|
|
2795
|
+
},
|
|
2796
|
+
})
|
|
2797
|
+
\`\`\`
|
|
2798
|
+
|
|
2799
|
+
Every request then carries \`authorization: Bearer <apikey>\`. An absent
|
|
2800
|
+
or empty key adds no header at all, so an API that needs no credentials
|
|
2801
|
+
is configured in exactly the same shape with an empty value — which is
|
|
2802
|
+
why it is worth writing even when there is nothing to send. An
|
|
2803
|
+
application that later moves to an authenticated service then changes one
|
|
2804
|
+
value rather than its structure.
|
|
2805
|
+
|
|
2806
|
+
For a different scheme, set the header yourself. Headers supplied through
|
|
2807
|
+
\`sdk\` win over the one the key would have set:
|
|
2808
|
+
|
|
2809
|
+
\`\`\`js
|
|
2810
|
+
.use('${provider.pkgName}', {
|
|
2811
|
+
sdk: { headers: { 'x-api-key': process.env.${provider.ENV}_APIKEY } },
|
|
2812
|
+
})
|
|
2813
|
+
\`\`\``)
|
|
2814
|
+
|
|
2815
|
+
|
|
2816
|
+
sec('Check which plugin and SDK are running', `One message, and the thing to reach for when a deployment is behaving
|
|
2817
|
+
unexpectedly:
|
|
2818
|
+
|
|
2819
|
+
\`\`\`js
|
|
2820
|
+
const info = await seneca.post(
|
|
2821
|
+
'sys:provider,provider:${provider.lower},get:info')
|
|
2822
|
+
\`\`\`
|
|
2823
|
+
|
|
2824
|
+
\`\`\`js
|
|
2825
|
+
{
|
|
2826
|
+
ok: true,
|
|
2827
|
+
name: '${provider.lower}',
|
|
2828
|
+
version: '${provider.version}',
|
|
2829
|
+
sdk: { name: '${provider.sdkPkg}', version: '${provider.sdkVersion}' },
|
|
2830
|
+
}
|
|
2831
|
+
\`\`\`
|
|
2832
|
+
|
|
2833
|
+
\`version\` is this plugin's; \`sdk.version\` is the SDK it is running
|
|
2834
|
+
against. That pair is what to quote in a bug report, because the two are
|
|
2835
|
+
released separately and most surprises live in the gap between them.`)
|
|
2836
|
+
|
|
2837
|
+
|
|
2838
|
+
{
|
|
2839
|
+
const dpe = eList || subject
|
|
2840
|
+
const dpath = dpe.path || provider.probePath || '/'
|
|
2841
|
+
const dparams = pathParams(dpath)
|
|
2842
|
+
const dval = (k: string) => (k === idf(dpe) || 'id' === k) ?
|
|
2843
|
+
`${dpe.name}0` : `${k.replace(/_id$/, '')}0`
|
|
2844
|
+
|
|
2845
|
+
sec('Reach the SDK directly', `The entity API covers the operations the API model declares. For
|
|
2846
|
+
anything else — an endpoint with no entity behind it, a response header
|
|
2847
|
+
you need to read — take the configured SDK client out of the plugin's
|
|
2848
|
+
exports:
|
|
2849
|
+
|
|
2850
|
+
\`\`\`js
|
|
2851
|
+
const sdk = seneca.export('${provider.pluginName}/sdk')()
|
|
2852
|
+
\`\`\`
|
|
2853
|
+
|
|
2854
|
+
The export is a function, so call it, and it only answers after
|
|
2855
|
+
\`seneca.ready()\` — that is when the plugin builds the client with the
|
|
2856
|
+
resolved key.
|
|
2857
|
+
|
|
2858
|
+
SDK operations resolve to SDK ENTITY instances rather than plain data, so
|
|
2859
|
+
read the record out with \`.data()\`. The provider does this for you; here
|
|
2860
|
+
you do it yourself:
|
|
2861
|
+
|
|
2862
|
+
\`\`\`js${dpe.cmds.includes('list') ? `
|
|
2863
|
+
const ${dpe.name}s = (await sdk.${dpe.acc}().list(${listArgs(dpe)}))
|
|
2864
|
+
.map((r) => r.data())
|
|
2865
|
+
` : ''}${dpe.cmds.includes('load') ? `
|
|
2866
|
+
const one = (await sdk.${dpe.acc}().load(${sdkLoadArgs(dpe)})).data()
|
|
2867
|
+
` : ''}\`\`\`
|
|
2868
|
+
|
|
2869
|
+
For a route the entity model does not cover at all, \`direct\` sends a
|
|
2870
|
+
request and hands back the raw response:
|
|
2871
|
+
|
|
2872
|
+
\`\`\`js
|
|
2873
|
+
const res = await sdk.direct({
|
|
2874
|
+
path: '${dpath}',
|
|
2875
|
+
method: 'GET',${0 === dparams.length ? '' : `
|
|
2876
|
+
params: { ${dparams.map((k: string) => `${key(k)}: '${dval(k)}'`).join(', ')} },`}
|
|
2877
|
+
})
|
|
2878
|
+
|
|
2879
|
+
if (res instanceof Error) throw res
|
|
2880
|
+
if (!res.ok) throw (res.err || new Error('status ' + res.status))
|
|
2881
|
+
|
|
2882
|
+
console.log(res.data)
|
|
2883
|
+
\`\`\`
|
|
2884
|
+
|
|
2885
|
+
\`prepare()\` builds the same request without sending it, which is the
|
|
2886
|
+
quickest way to see what the SDK would actually do — url, method, headers
|
|
2887
|
+
and body, before anything leaves the process.
|
|
2888
|
+
|
|
2889
|
+
Raw data becomes a Seneca entity again through \`data$\`:
|
|
2890
|
+
|
|
2891
|
+
\`\`\`js
|
|
2892
|
+
const ent = seneca.entity('${canon(dpe)}').data$(res.data)
|
|
2893
|
+
\`\`\``)
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
|
|
2897
|
+
sec('Develop against a local SDK checkout', `The SDK is an ordinary published dependency, so normal use needs nothing
|
|
2898
|
+
special:
|
|
2899
|
+
|
|
2900
|
+
\`\`\`sh
|
|
2901
|
+
$ npm install
|
|
2902
|
+
\`\`\`
|
|
2903
|
+
|
|
2904
|
+
If you are changing the SDK and this plugin together, point npm at a
|
|
2905
|
+
local checkout instead. Clone the SDK beside this repository, at the path
|
|
2906
|
+
this project expects, and build it — it does not commit its build output:
|
|
2907
|
+
|
|
2908
|
+
\`\`\`sh
|
|
2909
|
+
$ git clone ${provider.sdkRepoUrl}.git \\
|
|
2910
|
+
${provider.sdkrel}
|
|
2911
|
+
$ cd ${provider.sdkrel}/ts
|
|
2912
|
+
$ npm install && npm run build
|
|
2913
|
+
\`\`\`
|
|
2914
|
+
|
|
2915
|
+
Then link it in, without committing the change to \`package.json\`:
|
|
2916
|
+
|
|
2917
|
+
\`\`\`sh
|
|
2918
|
+
$ npm install --no-save ${provider.sdkrel}/ts
|
|
2919
|
+
\`\`\`
|
|
2920
|
+
|
|
2921
|
+
npm creates a symlink, so a rebuild of the SDK is picked up here with no
|
|
2922
|
+
reinstall:
|
|
2923
|
+
|
|
2924
|
+
\`\`\`sh
|
|
2925
|
+
$ ls -l node_modules/${provider.sdkPkg}
|
|
2926
|
+
\`\`\`
|
|
2927
|
+
|
|
2928
|
+
To go back to the published SDK:
|
|
2929
|
+
|
|
2930
|
+
\`\`\`sh
|
|
2931
|
+
$ rm -rf node_modules/${provider.sdkPkg} package-lock.json && npm install
|
|
2932
|
+
\`\`\`
|
|
2933
|
+
|
|
2934
|
+
Removing the lockfile matters. npm will happily keep resolving to the
|
|
2935
|
+
link if the lockfile still records it and the local version satisfies the
|
|
2936
|
+
range.`)
|
|
2937
|
+
|
|
2938
|
+
|
|
2939
|
+
{
|
|
2940
|
+
const pattern = subject.cmds.includes('load') ? `${subject.name}-load` :
|
|
2941
|
+
subject.cmds.includes('list') ? `${subject.name}-list` : 'happy'
|
|
2942
|
+
const skipped = subject.cmds.includes('list') ?
|
|
2943
|
+
`${subject.name}-list` : `${subject.name}-load-missing`
|
|
2944
|
+
|
|
2945
|
+
sec('Run the test suite', `\`\`\`sh
|
|
2946
|
+
$ npm run build
|
|
2947
|
+
$ npm test
|
|
2948
|
+
\`\`\`
|
|
2949
|
+
|
|
2950
|
+
The build comes first: the suite runs against \`dist\`, so an unbuilt
|
|
2951
|
+
change is not the change you are testing.
|
|
2952
|
+
|
|
2953
|
+
The offline tests use the SDK mock and always run.${'' === liveBase ? '' : ` The live tests
|
|
2954
|
+
probe for a server first and skip cleanly when there is none, so a clean
|
|
2955
|
+
checkout is green on a machine that has never started one:
|
|
2956
|
+
|
|
2957
|
+
\`\`\`
|
|
2958
|
+
﹣ ${skipped} # no ${provider.lower} server at ${liveBase}
|
|
2959
|
+
\`\`\``}
|
|
2960
|
+
|
|
2961
|
+
Coverage, and a single test by name:
|
|
2962
|
+
|
|
2963
|
+
\`\`\`sh
|
|
2964
|
+
$ npm run test-coverage
|
|
2965
|
+
$ TEST_PATTERN=${pattern} npm run test-some
|
|
2966
|
+
\`\`\``)
|
|
2967
|
+
}
|
|
2968
|
+
|
|
2969
|
+
|
|
2970
|
+
if ('' !== liveBase) {
|
|
2971
|
+
sec('Run the live tests against a server', `The companion test server ships only in the SDK's source repository, not
|
|
2972
|
+
in the published package. From the checkout beside this one:
|
|
2973
|
+
|
|
2974
|
+
\`\`\`sh
|
|
2975
|
+
$ cd ${provider.sdkrel}/app
|
|
2976
|
+
$ npm install && npm run build && npm start
|
|
2977
|
+
\`\`\`
|
|
2978
|
+
|
|
2979
|
+
Then run the suite as usual: the live tests find the server and activate
|
|
2980
|
+
themselves.
|
|
2981
|
+
|
|
2982
|
+
\`\`\`sh
|
|
2983
|
+
$ npm test
|
|
2984
|
+
\`\`\`
|
|
2985
|
+
|
|
2986
|
+
To target a server somewhere else:
|
|
2987
|
+
|
|
2988
|
+
\`\`\`sh
|
|
2989
|
+
$ ${provider.ENV}_TEST_BASE=http://localhost:9000 npm test
|
|
2990
|
+
\`\`\`
|
|
2991
|
+
|
|
2992
|
+
The generated live tests only read, so a run leaves the server exactly as
|
|
2993
|
+
it found it.${subject.cmds.includes('save') && subject.cmds.includes('remove') ? `
|
|
2994
|
+
|
|
2995
|
+
Two manual scripts are there for poking at a running server by hand:
|
|
2996
|
+
|
|
2997
|
+
\`\`\`sh
|
|
2998
|
+
$ node test/live.js # read from each entity
|
|
2999
|
+
$ node test/quick.js # a full write cycle, cleaning up after itself
|
|
3000
|
+
\`\`\`` : `
|
|
3001
|
+
|
|
3002
|
+
\`node test/live.js\` reads from each entity, for poking at a running
|
|
3003
|
+
server by hand.`}`)
|
|
3004
|
+
}
|
|
3005
|
+
|
|
3006
|
+
|
|
3007
|
+
sec('Build and release', `\`\`\`sh
|
|
3008
|
+
$ npm run build # tsc --build src test
|
|
3009
|
+
$ npm run watch # the same, in watch mode
|
|
3010
|
+
$ npm run reset # clean, install, build, test
|
|
3011
|
+
\`\`\`
|
|
3012
|
+
|
|
3013
|
+
Releasing follows the Seneca convention, in one command — clean, install,
|
|
3014
|
+
build, test, tag from \`package.json\`, publish:
|
|
3015
|
+
|
|
3016
|
+
\`\`\`sh
|
|
3017
|
+
$ npm run repo-publish
|
|
3018
|
+
\`\`\`
|
|
3019
|
+
|
|
3020
|
+
Only \`dist\`, the TypeScript sources and the licence file are published;
|
|
3021
|
+
the test suite and its build output stay in the repository.
|
|
3022
|
+
|
|
3023
|
+
Before publishing, check that \`package.json\` still depends on the
|
|
3024
|
+
published SDK by version range and not on a local path: a \`file:\`
|
|
3025
|
+
dependency left behind from local development installs perfectly on your
|
|
3026
|
+
own machine and cannot be resolved by anybody else.
|
|
3027
|
+
|
|
3028
|
+
One last thing: this repository is GENERATED from the ${provider.api} API
|
|
3029
|
+
model by [@voxgig/sdkgen](https://github.com/voxgig/sdkgen). An edit made
|
|
3030
|
+
here survives exactly as long as the next generation run. Change the
|
|
3031
|
+
model, or the components that build this target, and regenerate.`)
|
|
3032
|
+
|
|
3033
|
+
|
|
3034
|
+
File({ name: 'how-to.md' }, () => {
|
|
3035
|
+
Content(`# How-to guides
|
|
3036
|
+
|
|
3037
|
+
Each guide here solves one problem, and assumes you already have a
|
|
3038
|
+
working Seneca instance with this plugin loaded. If you do not, work
|
|
3039
|
+
through the [tutorial](tutorial.md) first.
|
|
3040
|
+
|
|
3041
|
+
These guides show what to do and leave out the reasoning — that is in the
|
|
3042
|
+
[explanation](explanation.md), and the exact patterns, fields and options
|
|
3043
|
+
are listed in the [reference](reference.md).
|
|
3044
|
+
|
|
3045
|
+
`)
|
|
3046
|
+
|
|
3047
|
+
each(sections, (s: any) => {
|
|
3048
|
+
Content(`- [${s.title}](${anchor(s.title)})
|
|
3049
|
+
`)
|
|
3050
|
+
})
|
|
3051
|
+
|
|
3052
|
+
each(sections, (s: any) => {
|
|
3053
|
+
Content(`
|
|
3054
|
+
## ${s.title}
|
|
3055
|
+
|
|
3056
|
+
${s.body}
|
|
3057
|
+
`)
|
|
3058
|
+
})
|
|
3059
|
+
})
|
|
3060
|
+
})
|
|
3061
|
+
|
|
3062
|
+
|
|
3063
|
+
// --- doc/reference.md ---------------------------------------------------------
|
|
3064
|
+
//
|
|
3065
|
+
// The Diátaxis reference: information-oriented, complete, and never teaching.
|
|
3066
|
+
// Everything a caller can reach — options, canons, fields, patterns, exports,
|
|
3067
|
+
// errors, environment variables, scripts — stated once, in tables, with the
|
|
3068
|
+
// exact strings the generated source actually emits.
|
|
3069
|
+
//
|
|
3070
|
+
// Three facts here are easy to get wrong by copying a hand-written original.
|
|
3071
|
+
// The guard message carries the PUBLISHED package name, because that is what
|
|
3072
|
+
// Main interpolates (`${provider.pkgName}: <entity> <cmd>: <key> is required`).
|
|
3073
|
+
// The `sdk` block of the get:info response carries the SDK's PACKAGE name, not
|
|
3074
|
+
// its slug. And an entity whose id field is not literally `id` cannot be read
|
|
3075
|
+
// with the `load$('x')` short form at all — Seneca turns that into `{id: 'x'}`,
|
|
3076
|
+
// which the generated action does not look at — so the object form is
|
|
3077
|
+
// documented for those entities instead of the string form.
|
|
3078
|
+
//
|
|
3079
|
+
// Nothing here assumes CRUD: every table row is conditional on the cmds and
|
|
3080
|
+
// ops the model actually declares, so an API offering only create, or only
|
|
3081
|
+
// reads, documents only what it has.
|
|
3082
|
+
|
|
3083
|
+
const DocReference = cmp(function DocReference(props: any) {
|
|
3084
|
+
const { provider } = props
|
|
3085
|
+
|
|
3086
|
+
// The entities exposing a custom action. Empty for an API whose every
|
|
3087
|
+
// route is CRUD, and the reference says so rather than omitting the
|
|
3088
|
+
// section — a reader who has seen `action$` elsewhere needs to be told it
|
|
3089
|
+
// has nothing to select here.
|
|
3090
|
+
const acting = provider.entities.filter((e: any) => 0 < e.actionList.length)
|
|
3091
|
+
|
|
3092
|
+
// The entity used for worked examples: the same choice the tests and README
|
|
3093
|
+
// make, so all three documents show the same entity.
|
|
3094
|
+
const subject = [...provider.entities]
|
|
3095
|
+
.sort((a: any, b: any) =>
|
|
3096
|
+
(a.parents.length - b.parents.length) || (b.cmds.length - a.cmds.length))[0]
|
|
3097
|
+
|
|
3098
|
+
const nested = provider.entities.filter((e: any) => 0 < e.parents.length)
|
|
3099
|
+
const saving = provider.entities.filter((e: any) => e.cmds.includes('save'))
|
|
3100
|
+
|
|
3101
|
+
// Entities whose API offers BOTH create and update: only for those does
|
|
3102
|
+
// `save$` dispatch on the id. Where it offers one, saying otherwise is wrong.
|
|
3103
|
+
const dispatch = saving.filter((e: any) =>
|
|
3104
|
+
e.ops.includes('create') && e.ops.includes('update'))
|
|
3105
|
+
const oneway = saving.filter((e: any) => !dispatch.includes(e))
|
|
3106
|
+
|
|
3107
|
+
const anyCmd = (c: string) => provider.entities.some((e: any) => e.cmds.includes(c))
|
|
3108
|
+
const anyOp = (o: string) => saving.some((e: any) => e.ops.includes(o))
|
|
3109
|
+
|
|
3110
|
+
const live = '' !== provider.liveBase
|
|
3111
|
+
|
|
3112
|
+
// test/quick.js is emitted only when the subject entity can be created and
|
|
3113
|
+
// removed again — see the Scripts cmp — so only document it when it exists.
|
|
3114
|
+
const quick = live && subject.cmds.includes('save') && subject.cmds.includes('remove')
|
|
3115
|
+
|
|
3116
|
+
const canon = (e: any) => `provider/${provider.lower}/${e.name}`
|
|
3117
|
+
|
|
3118
|
+
const cmdList = (e: any) => e.cmds.map((c: string) => '`' + c + '$`').join(', ')
|
|
3119
|
+
|
|
3120
|
+
const keys = (list: string[]) => '`' + list.join('`, `') + '`'
|
|
3121
|
+
|
|
3122
|
+
// The same list as prose: `a`, `b` and `c`.
|
|
3123
|
+
const keysAnd = (list: string[]) => 1 === list.length ? keys(list) :
|
|
3124
|
+
`${keys(list.slice(0, -1))} and \`${list[list.length - 1]}\``
|
|
3125
|
+
|
|
3126
|
+
// A query literal for the docs: parent keys first, then whatever else the
|
|
3127
|
+
// command needs.
|
|
3128
|
+
const query = (e: any, extra: string[]) =>
|
|
3129
|
+
`{ ${[...e.parents, ...extra].map((k: string) => `${k}: '...'`).join(', ')} }`
|
|
3130
|
+
|
|
3131
|
+
// How a single record is addressed. The `load$('x')` short form only works
|
|
3132
|
+
// when the id field is literally `id`.
|
|
3133
|
+
const oneArg = (e: any) => 0 < e.parents.length ? query(e, [e.idf]) :
|
|
3134
|
+
'id' === e.idf ? `'...'` : query(e, [e.idf])
|
|
3135
|
+
|
|
3136
|
+
// The required-key phrasing, which has to read correctly for one key as
|
|
3137
|
+
// well as several.
|
|
3138
|
+
const reqd = (list: string[]) =>
|
|
3139
|
+
1 === list.length ? `${keys(list)} **required**` :
|
|
3140
|
+
2 === list.length ? `\`${list[0]}\` and \`${list[1]}\`, both **required**` :
|
|
3141
|
+
`${keys(list)}, all **required**`
|
|
3142
|
+
|
|
3143
|
+
// The example used in the error block: whichever command the subject has.
|
|
3144
|
+
const errCall = subject.cmds.includes('list') ?
|
|
3145
|
+
`list$(${0 < subject.parents.length ? query(subject, []) : ''})` :
|
|
3146
|
+
subject.cmds.includes('load') ? `load$(${oneArg(subject)})` :
|
|
3147
|
+
subject.cmds.includes('remove') ? `remove$(${oneArg(subject)})` :
|
|
3148
|
+
'make$({ ... }).save$()'
|
|
3149
|
+
|
|
3150
|
+
File({ name: 'reference.md' }, () => {
|
|
3151
|
+
Content(`# Reference
|
|
3152
|
+
|
|
3153
|
+
Complete description of the interface exposed by
|
|
3154
|
+
\`${provider.pkgName}\` version ${provider.version}.
|
|
3155
|
+
|
|
3156
|
+
This document describes the machinery and assumes you know what you are
|
|
3157
|
+
looking for. To learn the plugin, start with the [tutorial](tutorial.md);
|
|
3158
|
+
for recipes, see the [how-to guides](how-to.md); for the reasoning behind
|
|
3159
|
+
the design, see the [explanation](explanation.md). The package overview is
|
|
3160
|
+
the [README](../README.md), and the document index is [here](README.md).
|
|
3161
|
+
|
|
3162
|
+
- [Requirements](#requirements)
|
|
3163
|
+
- [Registration](#registration)
|
|
3164
|
+
- [Options](#options)
|
|
3165
|
+
- [Entities](#entities)
|
|
3166
|
+
- [Actions](#actions)
|
|
3167
|
+
- [Action patterns](#action-patterns)
|
|
3168
|
+
- [Plugin exports](#plugin-exports)
|
|
3169
|
+
- [Errors](#errors)
|
|
3170
|
+
- [Authentication keys](#authentication-keys)
|
|
3171
|
+
- [Environment variables](#environment-variables)
|
|
3172
|
+
- [Package scripts](#package-scripts)
|
|
3173
|
+
|
|
3174
|
+
## Requirements
|
|
3175
|
+
|
|
3176
|
+
| Item | Value |
|
|
3177
|
+
| ---- | ----- |
|
|
3178
|
+
| Node.js | \`>=24\` |
|
|
3179
|
+
| Module format | CommonJS |
|
|
3180
|
+
| SDK | [\`${provider.sdkPkg}\`](https://www.npmjs.com/package/${provider.sdkPkg}) \`^${provider.sdkVersion}\` |
|
|
3181
|
+
|
|
3182
|
+
The SDK is an ordinary published dependency, installed by \`npm install\`
|
|
3183
|
+
like any other.
|
|
3184
|
+
`)
|
|
3185
|
+
|
|
3186
|
+
if (live) {
|
|
3187
|
+
Content(`
|
|
3188
|
+
The companion **test server** used by the live tests is a separate matter:
|
|
3189
|
+
it ships only in the SDK's [source repository](${provider.sdkRepoUrl}) under
|
|
3190
|
+
\`app/\`, and is not published. It is needed only to run the live tests —
|
|
3191
|
+
see the [how-to guides](how-to.md).
|
|
3192
|
+
`)
|
|
3193
|
+
}
|
|
3194
|
+
|
|
3195
|
+
Content(`
|
|
3196
|
+
### Peer dependencies
|
|
3197
|
+
|
|
3198
|
+
All must be present in the host application. The accepted version ranges are
|
|
3199
|
+
declared in this package's \`package.json\`.
|
|
3200
|
+
|
|
3201
|
+
| Package | Purpose |
|
|
3202
|
+
| ------- | ------- |
|
|
3203
|
+
| \`seneca\` | The host framework. The plugin runs inside the host's instance, never its own. |
|
|
3204
|
+
| \`seneca-entity\` | The entity API the canons below are served through. |
|
|
3205
|
+
| \`seneca-promisify\` | The promise-returning message API. |
|
|
3206
|
+
| \`@seneca/provider\` | The provider convention, including \`provider/entityBuilder\`. |
|
|
3207
|
+
| \`@seneca/env\` | Resolves \`$\`-prefixed key values from the environment. |
|
|
3208
|
+
|
|
3209
|
+
## Registration
|
|
3210
|
+
|
|
3211
|
+
The plugin name is \`${provider.pluginName}\`. It must be registered after
|
|
3212
|
+
\`entity\`, \`promisify\` and \`provider\`:
|
|
3213
|
+
|
|
3214
|
+
\`\`\`js
|
|
3215
|
+
Seneca({ legacy: false })
|
|
3216
|
+
.use('promisify')
|
|
3217
|
+
.use('entity')
|
|
3218
|
+
.use('provider', { ... })
|
|
3219
|
+
`)
|
|
3220
|
+
|
|
3221
|
+
if (live) {
|
|
3222
|
+
Content(` .use('${provider.pkgName}', { sdk: { base: '${provider.liveBase}' } })
|
|
3223
|
+
\`\`\`
|
|
3224
|
+
`)
|
|
3225
|
+
}
|
|
3226
|
+
else {
|
|
3227
|
+
Content(` .use('${provider.pkgName}', { sdk: { base: BASE } })
|
|
3228
|
+
\`\`\`
|
|
3229
|
+
|
|
3230
|
+
The ${provider.api} definition declares no server, so there is no default
|
|
3231
|
+
base URL: \`BASE\` is the URL of the API you are talking to, and it must be
|
|
3232
|
+
supplied through the \`sdk\` option.
|
|
3233
|
+
`)
|
|
3234
|
+
}
|
|
3235
|
+
|
|
3236
|
+
Content(`
|
|
3237
|
+
The SDK client is constructed during plugin startup and is not available
|
|
3238
|
+
until \`seneca.ready()\` resolves.
|
|
3239
|
+
|
|
3240
|
+
## Options
|
|
3241
|
+
|
|
3242
|
+
| Option | Type | Default | Effect |
|
|
3243
|
+
| ------ | ---- | ------- | ------ |
|
|
3244
|
+
| \`sdk\` | object | \`{}\` | Passed straight to the \`${provider.sdkClass}\` constructor. Most usefully \`base\`. |
|
|
3245
|
+
| \`test\` | boolean | \`false\` | Run the SDK against its in-memory mock transport instead of HTTP. |
|
|
3246
|
+
| \`testopts\` | object | \`{}\` | Test-feature options, used only when \`test\` is true. \`{entity: {...}}\` seeds the mock. |
|
|
3247
|
+
|
|
3248
|
+
### \`sdk\`
|
|
3249
|
+
|
|
3250
|
+
Any option the \`${provider.sdkClass}\` constructor accepts:
|
|
3251
|
+
|
|
3252
|
+
| Key | Effect |
|
|
3253
|
+
| --- | ------ |
|
|
3254
|
+
| \`base\` | Base URL for API requests. ${live ?
|
|
3255
|
+
`The SDK's own default is \`${provider.liveBase}\`.` :
|
|
3256
|
+
'There is no default: this API declares no server, so it must be set.'} |
|
|
3257
|
+
| \`prefix\` / \`suffix\` | URL fragments placed around the path. |
|
|
3258
|
+
| \`headers\` | Headers sent on every request. These win over the \`authorization\` header the provider adds from a configured key. |
|
|
3259
|
+
| \`system\` | System overrides, e.g. a custom \`fetch\`. |
|
|
3260
|
+
|
|
3261
|
+
### \`test\` and \`testopts\`
|
|
3262
|
+
|
|
3263
|
+
\`\`\`js
|
|
3264
|
+
.use('${provider.pkgName}', {
|
|
3265
|
+
test: true,
|
|
3266
|
+
testopts: {
|
|
3267
|
+
entity: {
|
|
3268
|
+
`)
|
|
3269
|
+
each(provider.entities, (e: any) => {
|
|
3270
|
+
Content(` ${e.name}: { ${e.name}0: ${JSON.stringify(seedRecord(e, 0))} },
|
|
3271
|
+
`)
|
|
3272
|
+
})
|
|
3273
|
+
Content(` },
|
|
3274
|
+
},
|
|
3275
|
+
})
|
|
3276
|
+
\`\`\`
|
|
3277
|
+
|
|
3278
|
+
Mock records are keyed by id under their entity name. In this mode no
|
|
3279
|
+
network calls are made, and an unseeded id produces the same not-found
|
|
3280
|
+
behaviour as a live server. This package's own \`test/seed.js\` is generated
|
|
3281
|
+
in exactly this shape.
|
|
3282
|
+
`)
|
|
3283
|
+
|
|
3284
|
+
if (0 < nested.length) {
|
|
3285
|
+
Content(`
|
|
3286
|
+
A nested record's parent key must name a record the parent entity also
|
|
3287
|
+
seeds: the mock resolves the path literally, so an unmatched parent id
|
|
3288
|
+
yields nothing rather than an error.
|
|
3289
|
+
`)
|
|
3290
|
+
}
|
|
3291
|
+
|
|
3292
|
+
Content(`
|
|
3293
|
+
## Entities
|
|
3294
|
+
|
|
3295
|
+
The plugin registers ${1 === provider.entities.length ?
|
|
3296
|
+
'one entity canon' : `${provider.entities.length} entity canons`}.
|
|
3297
|
+
A canon carries only the commands its API operations support — an entity the
|
|
3298
|
+
API offers no delete for has no \`remove$\` — so the tables below are the
|
|
3299
|
+
whole of what each one answers.
|
|
3300
|
+
|
|
3301
|
+
| Seneca canon | SDK accessor | Route | Id field | Parent keys | Commands |
|
|
3302
|
+
| ------------ | ------------ | ----- | -------- | ----------- | -------- |
|
|
3303
|
+
`)
|
|
3304
|
+
each(provider.entities, (e: any) => {
|
|
3305
|
+
Content(`| \`${canon(e)}\` | \`sdk.${e.acc}()\` | \`${e.path}\` | \`${e.idf}\` | ${0 < e.parents.length ?
|
|
3306
|
+
keys(e.parents) : '—'} | ${cmdList(e)} |
|
|
3307
|
+
`)
|
|
3308
|
+
})
|
|
3309
|
+
|
|
3310
|
+
each(provider.entities, (e: any) => {
|
|
3311
|
+
const hasCreate = e.ops.includes('create')
|
|
3312
|
+
const hasUpdate = e.ops.includes('update')
|
|
3313
|
+
|
|
3314
|
+
Content(`
|
|
3315
|
+
### \`${canon(e)}\`
|
|
3316
|
+
|
|
3317
|
+
Backed by \`sdk.${e.acc}()\`, whose results are \`${e.cls}\` instances; the
|
|
3318
|
+
provider hands Seneca the plain record from \`.data()\`.
|
|
3319
|
+
`)
|
|
3320
|
+
|
|
3321
|
+
if (0 < e.parents.length) {
|
|
3322
|
+
Content(`
|
|
3323
|
+
\`${e.name}\` is nested under \`${e.path}\` in the API, so **every**
|
|
3324
|
+
\`${e.name}\` command requires ${keysAnd(e.parents)}. Omitting one throws —
|
|
3325
|
+
\`${provider.pkgName}: ${e.name} <cmd>: ${e.parents[0]} is required\` —
|
|
3326
|
+
before any request is made, rather than issuing one that would 404.
|
|
3327
|
+
`)
|
|
3328
|
+
}
|
|
3329
|
+
|
|
3330
|
+
Content(`
|
|
3331
|
+
| Command | Query / data | Returns |
|
|
3332
|
+
| ------- | ------------ | ------- |
|
|
3333
|
+
`)
|
|
3334
|
+
if (e.cmds.includes('list')) {
|
|
3335
|
+
Content(`| \`list$(q)\` | ${0 < e.parents.length ?
|
|
3336
|
+
`${reqd(e.parents)}, plus optional match fields` :
|
|
3337
|
+
'optional match fields'} | Array of \`${e.name}\` entities. |
|
|
3338
|
+
`)
|
|
3339
|
+
}
|
|
3340
|
+
if (e.cmds.includes('load')) {
|
|
3341
|
+
Content(`| \`load$(q)\` | ${reqd([...e.parents, e.idf])} | One \`${e.name}\`, or \`null\` if not found. |
|
|
3342
|
+
`)
|
|
3343
|
+
}
|
|
3344
|
+
if (e.cmds.includes('save')) {
|
|
3345
|
+
Content(`| \`save$()\` | entity data${0 < e.parents.length ?
|
|
3346
|
+
`, including ${keysAnd(e.parents)}` : ''} | ${hasCreate && hasUpdate ?
|
|
3347
|
+
`Created or updated \`${e.name}\`.` : hasCreate ?
|
|
3348
|
+
`Created \`${e.name}\`; the API declares no update operation.` :
|
|
3349
|
+
`Updated \`${e.name}\`; the API declares no create operation.`} |
|
|
3350
|
+
`)
|
|
3351
|
+
}
|
|
3352
|
+
if (e.cmds.includes('remove')) {
|
|
3353
|
+
Content(`| \`remove$(q)\` | ${reqd([...e.parents, e.idf])} | \`null\`. |
|
|
3354
|
+
`)
|
|
3355
|
+
}
|
|
3356
|
+
|
|
3357
|
+
// The `load$('x')` short form sets `id`, which an entity keyed by
|
|
3358
|
+
// anything else never reads. Nested entities need the object form for
|
|
3359
|
+
// their parent keys anyway, so this only needs saying for top-level ones.
|
|
3360
|
+
const shortForm = 0 === e.parents.length && 'id' !== e.idf ?
|
|
3361
|
+
e.cmds.filter((c: string) => 'load' === c || 'remove' === c) : []
|
|
3362
|
+
|
|
3363
|
+
if (0 < shortForm.length) {
|
|
3364
|
+
Content(`
|
|
3365
|
+
This entity is keyed by \`${e.idf}\` rather than \`id\`, so the short
|
|
3366
|
+
${1 === shortForm.length ? 'form' : 'forms'} ${shortForm
|
|
3367
|
+
.map((c: string) => `\`${c}$('...')\``).join(' and ')} ${1 === shortForm.length ?
|
|
3368
|
+
'does' : 'do'} not address it: Seneca reads a bare string as
|
|
3369
|
+
\`{id: '...'}\`, which is not a key this entity uses. Pass
|
|
3370
|
+
\`{ ${e.idf}: '...' }\` instead.
|
|
3371
|
+
`)
|
|
3372
|
+
}
|
|
3373
|
+
|
|
3374
|
+
if (0 === e.fields.length) {
|
|
3375
|
+
Content(`
|
|
3376
|
+
The API definition declares no required fields for this entity; whatever it
|
|
3377
|
+
returns is passed through unchanged.
|
|
3378
|
+
`)
|
|
3379
|
+
}
|
|
3380
|
+
else {
|
|
3381
|
+
Content(`
|
|
3382
|
+
Required fields, as declared by the API definition. Optional fields the API
|
|
3383
|
+
also defines are passed through unchanged in both directions.
|
|
3384
|
+
|
|
3385
|
+
| Field | Type | Notes |
|
|
3386
|
+
| ----- | ---- | ----- |
|
|
3387
|
+
`)
|
|
3388
|
+
each(e.fields, (f: any) => {
|
|
3389
|
+
Content(`| \`${f.name}\` | ${f.kind} | ${f.name === e.idf ? 'Id field.' :
|
|
3390
|
+
e.parents.includes(f.name) ? ('' === f.parentEntity ?
|
|
3391
|
+
'Parent key. Required by every command.' :
|
|
3392
|
+
`Parent key: the id of a \`${f.parentEntity}\`. Required by every command.`) : ''} |
|
|
3393
|
+
`)
|
|
3394
|
+
})
|
|
3395
|
+
}
|
|
3396
|
+
|
|
3397
|
+
if (e.cmds.includes('list') || e.cmds.includes('load')) {
|
|
3398
|
+
Content(`
|
|
3399
|
+
\`\`\`js
|
|
3400
|
+
`)
|
|
3401
|
+
if (e.cmds.includes('list')) {
|
|
3402
|
+
Content(`const ${e.name}s = await seneca
|
|
3403
|
+
.entity('${canon(e)}')
|
|
3404
|
+
.list$(${0 < e.parents.length ? query(e, []) : ''})
|
|
3405
|
+
`)
|
|
3406
|
+
}
|
|
3407
|
+
if (e.cmds.includes('load')) {
|
|
3408
|
+
Content(`const ${e.name} = await seneca
|
|
3409
|
+
.entity('${canon(e)}')
|
|
3410
|
+
.load$(${oneArg(e)})
|
|
3411
|
+
`)
|
|
3412
|
+
}
|
|
3413
|
+
Content(`\`\`\`
|
|
3414
|
+
`)
|
|
3415
|
+
}
|
|
3416
|
+
})
|
|
3417
|
+
|
|
3418
|
+
if (0 < dispatch.length) {
|
|
3419
|
+
// The dispatching entity to show it with: the subject when it qualifies,
|
|
3420
|
+
// otherwise the first that does.
|
|
3421
|
+
const s = dispatch.includes(subject) ? subject : dispatch[0]
|
|
3422
|
+
const writable = s.fields
|
|
3423
|
+
.filter((f: any) => f.name !== s.idf && f.name !== 'id')
|
|
3424
|
+
.filter((f: any) => !s.parents.includes(f.name))
|
|
3425
|
+
const value = (f: any, alt: boolean) => 'number' === f.kind ?
|
|
3426
|
+
(alt ? '4321' : '1234') : 'boolean' === f.kind ?
|
|
3427
|
+
(alt ? 'true' : 'false') : `'${f.name}${alt ? '-changed' : '-value'}'`
|
|
3428
|
+
const make = [
|
|
3429
|
+
...s.parents.map((k: string) => `${k}: '...'`),
|
|
3430
|
+
...writable.map((f: any) => `${f.name}: ${value(f, false)}`),
|
|
3431
|
+
].join(', ')
|
|
3432
|
+
|
|
3433
|
+
Content(`
|
|
3434
|
+
### Create versus update
|
|
3435
|
+
|
|
3436
|
+
\`save$\` follows the Seneca convention: an entity **without** an id is
|
|
3437
|
+
created, an entity **with** one is updated. The provider dispatches on the
|
|
3438
|
+
id field, so the same call does both.
|
|
3439
|
+
|
|
3440
|
+
\`\`\`js
|
|
3441
|
+
// Create — no ${s.idf}.
|
|
3442
|
+
const ${s.name} = await seneca
|
|
3443
|
+
.entity('${canon(s)}')
|
|
3444
|
+
.make$({ ${make} })
|
|
3445
|
+
.save$()
|
|
3446
|
+
|
|
3447
|
+
// Update — ${s.idf} present.
|
|
3448
|
+
${0 < writable.length ? `${s.name}.${writable[0].name} = ${value(writable[0], true)}
|
|
3449
|
+
` : ''}await ${s.name}.save$()
|
|
3450
|
+
\`\`\`
|
|
3451
|
+
|
|
3452
|
+
Whether a client-supplied id survives a create is a property of the API, not
|
|
3453
|
+
of this plugin: many assign the id themselves and ignore the one sent. Read
|
|
3454
|
+
the id back off the returned entity rather than assuming the one you set.
|
|
3455
|
+
`)
|
|
3456
|
+
}
|
|
3457
|
+
|
|
3458
|
+
if (0 < oneway.length) {
|
|
3459
|
+
if (0 === dispatch.length) {
|
|
3460
|
+
Content(`
|
|
3461
|
+
### Create versus update
|
|
3462
|
+
|
|
3463
|
+
\`save$\` normally dispatches on the id: an entity without one is created,
|
|
3464
|
+
an entity with one is updated.
|
|
3465
|
+
`)
|
|
3466
|
+
}
|
|
3467
|
+
|
|
3468
|
+
Content(`
|
|
3469
|
+
${1 === oneway.length ?
|
|
3470
|
+
'This entity supports only one half of that pair, so `save$` does not' :
|
|
3471
|
+
'These entities support only one half of that pair, so `save$` does not'}
|
|
3472
|
+
dispatch for ${1 === oneway.length ? 'it' : 'them'}:
|
|
3473
|
+
|
|
3474
|
+
| Canon | Behaviour of \`save$\` |
|
|
3475
|
+
| ----- | -------------------- |
|
|
3476
|
+
`)
|
|
3477
|
+
each(oneway, (e: any) => {
|
|
3478
|
+
Content(`| \`${canon(e)}\` | Always ${e.ops.includes('create') ? 'creates' : 'updates'}; the API declares no ${e.ops.includes('create') ? 'update' : 'create'} operation. |
|
|
3479
|
+
`)
|
|
3480
|
+
})
|
|
3481
|
+
}
|
|
3482
|
+
|
|
3483
|
+
Content(`
|
|
3484
|
+
### Command to SDK operation
|
|
3485
|
+
|
|
3486
|
+
| Seneca command | SDK call | Notes |
|
|
3487
|
+
| -------------- | -------- | ----- |
|
|
3488
|
+
`)
|
|
3489
|
+
if (anyCmd('list')) {
|
|
3490
|
+
Content(`| \`list$(q)\` | \`.list(q)\` | Query keys are passed through as match fields. |
|
|
3491
|
+
`)
|
|
3492
|
+
}
|
|
3493
|
+
if (anyCmd('load')) {
|
|
3494
|
+
Content(`| \`load$(q)\` | \`.load({ ...keys })\` | Only the keys the route needs are sent. |
|
|
3495
|
+
`)
|
|
3496
|
+
}
|
|
3497
|
+
if (anyOp('create')) {
|
|
3498
|
+
Content(`| \`save$()\` on an entity with no id | \`.create(data)\` | Data is the entity's own fields, without Seneca metadata. |
|
|
3499
|
+
`)
|
|
3500
|
+
}
|
|
3501
|
+
if (anyOp('update')) {
|
|
3502
|
+
Content(`| \`save$()\` on an entity with an id | \`.update(data)\` | |
|
|
3503
|
+
`)
|
|
3504
|
+
}
|
|
3505
|
+
if (anyCmd('remove')) {
|
|
3506
|
+
Content(`| \`remove$(q)\` | \`.remove({ ...keys })\` | Resolves to \`null\` whatever the API returns. |
|
|
3507
|
+
`)
|
|
3508
|
+
}
|
|
3509
|
+
|
|
3510
|
+
Content(`
|
|
3511
|
+
Every SDK operation resolves to an SDK entity instance, or a list of them,
|
|
3512
|
+
rather than raw data. The provider calls \`.data()\` on each and hands the
|
|
3513
|
+
plain record to \`entize\`, so what comes back is an ordinary Seneca entity
|
|
3514
|
+
under this plugin's canon, carrying none of the SDK's own markers.
|
|
3515
|
+
|
|
3516
|
+
### Query fields
|
|
3517
|
+
|
|
3518
|
+
Seneca query directives — any key ending in \`$\`, such as \`sort$\` or
|
|
3519
|
+
\`limit$\` — are stripped before the query reaches the SDK. They are
|
|
3520
|
+
instructions to a store, not match fields for the API, and are not
|
|
3521
|
+
otherwise supported.
|
|
3522
|
+
|
|
3523
|
+
\`action$\` is the one this plugin reads. It is stripped from the match
|
|
3524
|
+
fields like the rest, but it is read FIRST, and it selects a custom API
|
|
3525
|
+
action instead of the plain command. See
|
|
3526
|
+
[Actions](#actions) below.
|
|
3527
|
+
|
|
3528
|
+
### Actions
|
|
3529
|
+
${0 === acting.length ? `
|
|
3530
|
+
This API declares no custom actions: every route is one of the five CRUD
|
|
3531
|
+
operations, so \`action$\` has nothing to select and naming one throws.
|
|
3532
|
+
` : `
|
|
3533
|
+
An action is an API route folded into an ordinary operation as an
|
|
3534
|
+
alternative point — a verb that is not create, read, update or delete.
|
|
3535
|
+
Select one with the \`action$\` directive; the rest of the call is that
|
|
3536
|
+
action's own payload.
|
|
3537
|
+
|
|
3538
|
+
| Entity | Action | Route | Operation | Command |
|
|
3539
|
+
| --- | --- | --- | --- | --- |
|
|
3540
|
+
${acting.map((e: any) => e.actionList.map((a: any) =>
|
|
3541
|
+
`| \`${e.name}\` | \`${a.action}\` | \`${a.path}\` | \`${a.op}\` | \`${a.cmd}$\` |`)
|
|
3542
|
+
.join('\n')).join('\n')}
|
|
3543
|
+
|
|
3544
|
+
On a read command (\`list$\`, \`load$\`, \`remove$\`) \`action$\` is a key of
|
|
3545
|
+
the query. On \`save$\` it is a directive on the entity, set with
|
|
3546
|
+
\`directive$({ action$: '...' })\` or assigned as a property —
|
|
3547
|
+
\`make$({ action$ })\` does NOT work, because \`seneca-entity\`'s \`make$\`
|
|
3548
|
+
drops any trailing-\`$\` key it does not know by name.
|
|
3549
|
+
|
|
3550
|
+
Routing is by the operation the action belongs to, not by the command:
|
|
3551
|
+
\`save$\` covers both create and update, so an action folded into \`create\`
|
|
3552
|
+
is called as a create even when the entity carries an id.
|
|
3553
|
+
|
|
3554
|
+
An action name the entity does not have throws, naming the entity, the
|
|
3555
|
+
command and the valid actions. It never falls back to the plain command.
|
|
3556
|
+
`}
|
|
3557
|
+
|
|
3558
|
+
## Action patterns
|
|
3559
|
+
|
|
3560
|
+
### \`sys:provider,provider:${provider.lower},get:info\`
|
|
3561
|
+
|
|
3562
|
+
Returns metadata about the plugin and SDK. Answered locally; makes no API
|
|
3563
|
+
call.
|
|
3564
|
+
|
|
3565
|
+
\`\`\`js
|
|
3566
|
+
await seneca.post('sys:provider,provider:${provider.lower},get:info')
|
|
3567
|
+
\`\`\`
|
|
3568
|
+
|
|
3569
|
+
\`\`\`js
|
|
3570
|
+
{
|
|
3571
|
+
ok: true,
|
|
3572
|
+
name: '${provider.lower}',
|
|
3573
|
+
version: '${provider.version}',
|
|
3574
|
+
sdk: {
|
|
3575
|
+
name: '${provider.sdkPkg}',
|
|
3576
|
+
version: '${provider.sdkVersion}',
|
|
3577
|
+
},
|
|
3578
|
+
}
|
|
3579
|
+
\`\`\`
|
|
3580
|
+
|
|
3581
|
+
Both versions are read at runtime from the respective \`package.json\`, so
|
|
3582
|
+
they describe what is installed rather than what was generated.
|
|
3583
|
+
|
|
3584
|
+
### Entity patterns
|
|
3585
|
+
|
|
3586
|
+
Registered by \`@seneca/provider\`. Normally reached through the entity API
|
|
3587
|
+
rather than posted directly.
|
|
3588
|
+
|
|
3589
|
+
| Pattern |
|
|
3590
|
+
| ------- |
|
|
3591
|
+
`)
|
|
3592
|
+
each(provider.entities, (e: any) => {
|
|
3593
|
+
Content(e.cmds
|
|
3594
|
+
.map((c: string) =>
|
|
3595
|
+
`| \`sys:entity,zone:provider,base:${provider.lower},name:${e.name},cmd:${c}\` |\n`)
|
|
3596
|
+
.join(''))
|
|
3597
|
+
})
|
|
3598
|
+
|
|
3599
|
+
Content(`
|
|
3600
|
+
### Inherited from \`@seneca/provider\`
|
|
3601
|
+
|
|
3602
|
+
| Pattern | Purpose |
|
|
3603
|
+
| ------- | ------- |
|
|
3604
|
+
| \`sys:provider,get:key\` | Fetch one named key for a provider. |
|
|
3605
|
+
| \`sys:provider,get:keymap\` | Fetch all keys for a provider. |
|
|
3606
|
+
| \`sys:provider,list:provider\` | List registered providers and their key names. |
|
|
3607
|
+
|
|
3608
|
+
## Plugin exports
|
|
3609
|
+
|
|
3610
|
+
### \`${provider.pluginName}/sdk\`
|
|
3611
|
+
|
|
3612
|
+
A function returning the configured \`${provider.sdkClass}\` instance.
|
|
3613
|
+
|
|
3614
|
+
\`\`\`js
|
|
3615
|
+
const sdk = seneca.export('${provider.pluginName}/sdk')()
|
|
3616
|
+
`)
|
|
3617
|
+
if (subject.ops.includes('list')) {
|
|
3618
|
+
Content(`
|
|
3619
|
+
// Every SDK operation resolves to an SDK entity (or a list of them),
|
|
3620
|
+
// not raw data; \`.data()\` gives the plain record.
|
|
3621
|
+
const ${subject.name}s = (await sdk.${subject.acc}().list()).map((e) => e.data())
|
|
3622
|
+
`)
|
|
3623
|
+
}
|
|
3624
|
+
if ('' !== provider.probePath) {
|
|
3625
|
+
Content(`
|
|
3626
|
+
// \`direct\` reaches endpoints outside the entity model.
|
|
3627
|
+
const res = await sdk.direct({ path: '${provider.probePath}', method: 'GET' })
|
|
3628
|
+
`)
|
|
3629
|
+
}
|
|
3630
|
+
Content(`\`\`\`
|
|
3631
|
+
|
|
3632
|
+
Available only after \`seneca.ready()\`. Use it for SDK features the entity
|
|
3633
|
+
API does not surface — notably \`direct()\` and \`prepare()\` for endpoints
|
|
3634
|
+
the entity model does not cover.
|
|
3635
|
+
|
|
3636
|
+
## Errors
|
|
3637
|
+
|
|
3638
|
+
| Situation | Behaviour |
|
|
3639
|
+
| --------- | --------- |
|
|
3640
|
+
`)
|
|
3641
|
+
if (anyCmd('load')) {
|
|
3642
|
+
Content(`| \`load$\` for a non-existent id | Resolves to \`null\`. |
|
|
3643
|
+
`)
|
|
3644
|
+
}
|
|
3645
|
+
if (anyCmd('remove')) {
|
|
3646
|
+
Content(`| \`remove$\` for a non-existent id | Resolves to \`null\`; not an error. |
|
|
3647
|
+
`)
|
|
3648
|
+
}
|
|
3649
|
+
if (0 < nested.length) {
|
|
3650
|
+
Content(`| A nested entity command missing a parent key | Throws before any request is made. |
|
|
3651
|
+
`)
|
|
3652
|
+
}
|
|
3653
|
+
if (anyCmd('list') || anyCmd('save')) {
|
|
3654
|
+
Content(`| A 404 from \`${[anyCmd('list') ? 'list$' : '', anyCmd('save') ? 'save$' : ''].filter((s: string) => '' !== s).join('` or `')}\` | Thrown. Only single-record reads and removes map a 404 to \`null\`. |
|
|
3655
|
+
`)
|
|
3656
|
+
}
|
|
3657
|
+
Content(`| Any other non-2xx response | Thrown as raised by the SDK. |
|
|
3658
|
+
| A request that never got a response | Thrown, with \`status\` \`-1\`. |
|
|
3659
|
+
|
|
3660
|
+
SDK errors are \`${provider.Name}Error\` instances carrying
|
|
3661
|
+
\`is${provider.Name}Error: true\`, a \`code\` (e.g. \`request_status\`), the
|
|
3662
|
+
HTTP \`status\` at the top level (\`-1\` when the request never got a
|
|
3663
|
+
response), a \`notFound\` flag, and a \`ctx\` holding the request context and
|
|
3664
|
+
its \`result\` — \`status\`, \`statusText\`, \`headers\` and \`body\`. The
|
|
3665
|
+
\`null\`-on-missing behaviour is triggered by \`err.notFound\`, not by
|
|
3666
|
+
inspecting the status at the call site.
|
|
3667
|
+
|
|
3668
|
+
\`\`\`js
|
|
3669
|
+
try {
|
|
3670
|
+
await seneca.entity('${canon(subject)}').${errCall}
|
|
3671
|
+
}
|
|
3672
|
+
catch (err) {
|
|
3673
|
+
console.error(err.code, err.status, err.notFound)
|
|
3674
|
+
}
|
|
3675
|
+
\`\`\`
|
|
3676
|
+
`)
|
|
3677
|
+
|
|
3678
|
+
if (0 < nested.length) {
|
|
3679
|
+
Content(`
|
|
3680
|
+
The missing-parent-key guard is this plugin's own, thrown before the SDK is
|
|
3681
|
+
called at all. Its message names the entity, the command and the key:
|
|
3682
|
+
|
|
3683
|
+
| Entity | Message |
|
|
3684
|
+
| ------ | ------- |
|
|
3685
|
+
`)
|
|
3686
|
+
each(nested, (e: any) => {
|
|
3687
|
+
Content(e.parents
|
|
3688
|
+
.map((k: string) =>
|
|
3689
|
+
`| \`${e.name}\` | \`${provider.pkgName}: ${e.name} <cmd>: ${k} is required\` |\n`)
|
|
3690
|
+
.join(''))
|
|
3691
|
+
})
|
|
3692
|
+
Content(`
|
|
3693
|
+
where \`<cmd>\` is the command that was called. A key counts as missing if
|
|
3694
|
+
it is absent, \`null\` or the empty string.
|
|
3695
|
+
`)
|
|
3696
|
+
}
|
|
3697
|
+
|
|
3698
|
+
Content(`
|
|
3699
|
+
## Authentication keys
|
|
3700
|
+
|
|
3701
|
+
The plugin follows the provider convention: if an \`apikey\` key is
|
|
3702
|
+
configured and non-empty, it is sent as \`authorization: Bearer <apikey>\`
|
|
3703
|
+
on every request. If the provider is not registered, or the key is absent or
|
|
3704
|
+
empty, no header is added and startup proceeds normally — an API that needs
|
|
3705
|
+
no credential exercises the same path.
|
|
3706
|
+
|
|
3707
|
+
\`\`\`js
|
|
3708
|
+
.use('provider', {
|
|
3709
|
+
provider: {
|
|
3710
|
+
${provider.lower}: {
|
|
3711
|
+
keys: {
|
|
3712
|
+
apikey: { value: '$${provider.ENV}_APIKEY' },
|
|
3713
|
+
},
|
|
3714
|
+
},
|
|
3715
|
+
},
|
|
3716
|
+
})
|
|
3717
|
+
\`\`\`
|
|
3718
|
+
|
|
3719
|
+
The key is read once, during \`seneca.prepare()\`, by posting
|
|
3720
|
+
\`sys:provider,get:keymap,provider:${provider.lower}\`. An \`authorization\`
|
|
3721
|
+
header supplied through the \`sdk.headers\` option takes precedence over it.
|
|
3722
|
+
|
|
3723
|
+
## Environment variables
|
|
3724
|
+
|
|
3725
|
+
The plugin never reads the environment itself. These are the variables the
|
|
3726
|
+
surrounding convention and tooling resolve:
|
|
3727
|
+
|
|
3728
|
+
| Variable | Read by | Purpose |
|
|
3729
|
+
| -------- | ------- | ------- |
|
|
3730
|
+
| \`$${provider.ENV}_APIKEY\` | \`@seneca/env\` | Supplies the \`apikey\` value when the key is declared as \`'$${provider.ENV}_APIKEY'\`, as above. |
|
|
3731
|
+
`)
|
|
3732
|
+
if (live) {
|
|
3733
|
+
Content(`| \`$${provider.ENV}_TEST_BASE\` | The test suite and the manual scripts | Base URL for the live tests. Defaults to \`${provider.liveBase}\`. |
|
|
3734
|
+
`)
|
|
3735
|
+
}
|
|
3736
|
+
|
|
3737
|
+
Content(`
|
|
3738
|
+
## Package scripts
|
|
3739
|
+
|
|
3740
|
+
| Script | Action |
|
|
3741
|
+
| ------ | ------ |
|
|
3742
|
+
| \`npm run build\` | \`tsc --build src test\` — compiles to \`dist\` and \`dist-test\`. |
|
|
3743
|
+
| \`npm run watch\` | The same, in watch mode. |
|
|
3744
|
+
| \`npm test\` | Runs the \`node:test\` suite. |
|
|
3745
|
+
| \`npm run test-some\` | Runs tests matching \`$TEST_PATTERN\`. |
|
|
3746
|
+
| \`npm run test-watch\` | Test suite in watch mode. |
|
|
3747
|
+
| \`npm run test-coverage\` | Test suite with Node's built-in coverage. |
|
|
3748
|
+
| \`npm run clean\` | Removes \`node_modules\`, \`dist\`, \`dist-test\`, \`.tsbuildinfo\`, lockfiles. |
|
|
3749
|
+
| \`npm run reset\` | \`clean\`, then install, build and test. |
|
|
3750
|
+
| \`npm run repo-tag\` | Commits, tags and pushes \`v<version>\` taken from \`package.json\`. |
|
|
3751
|
+
| \`npm run repo-publish\` | Clean install, then \`repo-publish-quick\`. |
|
|
3752
|
+
| \`npm run repo-publish-quick\` | Build, test, tag, and publish to npm. |
|
|
3753
|
+
|
|
3754
|
+
### Repository layout
|
|
3755
|
+
|
|
3756
|
+
| Path | Contents |
|
|
3757
|
+
| ---- | -------- |
|
|
3758
|
+
| \`src/\` | TypeScript source, with its own \`tsconfig.json\`. |
|
|
3759
|
+
| \`test/\` | Test suite (\`.js\`, run by \`node:test\`) and TypeScript fixtures. |
|
|
3760
|
+
| \`dist/\` | Compiled source. Committed; published. |
|
|
3761
|
+
| \`dist-test/\` | Compiled test fixtures. Committed; **not** published. |
|
|
3762
|
+
| \`.tsbuildinfo/\` | Incremental build cache. Not committed. |
|
|
3763
|
+
| \`doc/\` | This documentation. |
|
|
3764
|
+
|
|
3765
|
+
This repository is generated by
|
|
3766
|
+
[@voxgig/sdkgen](https://github.com/voxgig/sdkgen) from the ${provider.api}
|
|
3767
|
+
API definition. Anything edited here is overwritten by the next generation
|
|
3768
|
+
run; changes belong in the model.
|
|
3769
|
+
`)
|
|
3770
|
+
|
|
3771
|
+
if (live) {
|
|
3772
|
+
const listable = provider.entities
|
|
3773
|
+
.filter((e: any) => e.cmds.includes('list'))
|
|
3774
|
+
.map((e: any) => e.name)
|
|
3775
|
+
|
|
3776
|
+
Content(`
|
|
3777
|
+
### Manual scripts
|
|
3778
|
+
|
|
3779
|
+
Not part of \`npm test\`: they need the companion test server, which is
|
|
3780
|
+
distributed only in the SDK's source repository.
|
|
3781
|
+
|
|
3782
|
+
| Script | Purpose |
|
|
3783
|
+
| ------ | ------ |
|
|
3784
|
+
`)
|
|
3785
|
+
Content(`| \`node test/live.js\` | ${0 < listable.length ?
|
|
3786
|
+
`Read ${listable.join(', ')} from a running server.` :
|
|
3787
|
+
'Reads from a running server; no entity here supports `list$`, so it does nothing.'} |
|
|
3788
|
+
`)
|
|
3789
|
+
if (quick) {
|
|
3790
|
+
Content(`| \`node test/quick.js\` | Exercise the full write cycle on \`${subject.name}\`, cleaning up after itself. |
|
|
3791
|
+
`)
|
|
3792
|
+
}
|
|
3793
|
+
Content(`
|
|
3794
|
+
${quick ? 'Both scripts target' : 'It targets'} \`$${provider.ENV}_TEST_BASE\`, defaulting to
|
|
3795
|
+
\`${provider.liveBase}\`.
|
|
3796
|
+
`)
|
|
3797
|
+
}
|
|
3798
|
+
})
|
|
3799
|
+
})
|
|
3800
|
+
|
|
3801
|
+
|
|
3802
|
+
// --- doc/explanation.md ------------------------------------------------------
|
|
3803
|
+
//
|
|
3804
|
+
// The understanding-oriented corner of the Diátaxis set: the document someone
|
|
3805
|
+
// opens when the plugin surprised them. It DISCUSSES and never instructs, so
|
|
3806
|
+
// nothing here is a step and nothing here is a table — those belong in
|
|
3807
|
+
// tutorial.md, how-to.md and reference.md.
|
|
3808
|
+
//
|
|
3809
|
+
// The hard part of generating this one is that its subject is design reasoning,
|
|
3810
|
+
// most of which is true of EVERY provider this target emits (the entityBuilder
|
|
3811
|
+
// convention, the four-cmds-to-five-ops join, the .data() hop, the 404
|
|
3812
|
+
// translation) and only some of which depends on the model (whether any entity
|
|
3813
|
+
// is nested, whether writes exist at all, whether the API declares a server).
|
|
3814
|
+
// So the invariant prose is written once and the model-dependent sections are
|
|
3815
|
+
// guarded — an API with no nesting gets no nesting section rather than a
|
|
3816
|
+
// section explaining that it has none.
|
|
3817
|
+
|
|
3818
|
+
const DocExplanation = cmp(function DocExplanation(props: any) {
|
|
3819
|
+
const { provider } = props
|
|
3820
|
+
|
|
3821
|
+
// The entity used as the worked example throughout: fewest parent keys
|
|
3822
|
+
// (nothing to arrange around it) and the most cmds. Same choice the Tests
|
|
3823
|
+
// and Readme cmps make, so the documents agree on what they talk about.
|
|
3824
|
+
const subject = [...provider.entities]
|
|
3825
|
+
.sort((a: any, b: any) =>
|
|
3826
|
+
(a.parents.length - b.parents.length) || (b.cmds.length - a.cmds.length))[0]
|
|
3827
|
+
|
|
3828
|
+
const nested = provider.entities.filter((e: any) => 0 < e.parents.length)
|
|
3829
|
+
const writable = provider.entities.filter((e: any) => e.cmds.includes('save'))
|
|
3830
|
+
const loadable = provider.entities.filter((e: any) => e.cmds.includes('load'))
|
|
3831
|
+
const removable = provider.entities.filter((e: any) => e.cmds.includes('remove'))
|
|
3832
|
+
|
|
3833
|
+
// Entities where `save` has nothing to dispatch on, because the API offers
|
|
3834
|
+
// only one of create/update. Worth naming: their `save$` ignores the id
|
|
3835
|
+
// rule the rest of this document explains.
|
|
3836
|
+
const onesided = writable.filter((e: any) =>
|
|
3837
|
+
!(e.ops.includes('create') && e.ops.includes('update')))
|
|
3838
|
+
|
|
3839
|
+
const code = (s: string) => '`' + s + '`'
|
|
3840
|
+
const list = (names: string[]) => names.map(code).join(', ')
|
|
3841
|
+
|
|
3842
|
+
// What the offline suite actually covers, so the prose does not claim a
|
|
3843
|
+
// `load` test for an entity that has no load.
|
|
3844
|
+
const covered: string[] = []
|
|
3845
|
+
if (subject.cmds.includes('list')) covered.push('list')
|
|
3846
|
+
if (subject.cmds.includes('load')) covered.push('load', 'the not-found answer')
|
|
3847
|
+
if (0 < nested.length) covered.push('the nested-entity rules')
|
|
3848
|
+
const coveredPhrase = 0 < covered.length ? ` — ${covered.join(', ')} —` : ''
|
|
3849
|
+
|
|
3850
|
+
const othersSentence = 1 < provider.entities.length ?
|
|
3851
|
+
'The other entities carry whatever their own operations support; the\n' +
|
|
3852
|
+
'[reference](reference.md) lists them all.' :
|
|
3853
|
+
'It is the only entity this API declares, and the\n' +
|
|
3854
|
+
'[reference](reference.md) spells its commands out.'
|
|
3855
|
+
|
|
3856
|
+
// The nesting section names the parent entity when the model knows it, and
|
|
3857
|
+
// falls back to the path params when a parent key points at nothing declared.
|
|
3858
|
+
const n = nested[0]
|
|
3859
|
+
const nestLead = null == n ? '' :
|
|
3860
|
+
'' !== n.parentEntity ?
|
|
3861
|
+
`The API nests ${code(n.name)} under ${code(n.parentEntity)}: a ` +
|
|
3862
|
+
`${code(n.name)}'s URL contains its ${code(n.parentEntity)}.` :
|
|
3863
|
+
`The API nests ${code(n.name)} under a parent resource: a ` +
|
|
3864
|
+
`${code(n.name)}'s URL contains ${list(n.parents)}.`
|
|
3865
|
+
|
|
3866
|
+
File({ name: 'explanation.md' }, () => {
|
|
3867
|
+
Content(`# Explanation
|
|
3868
|
+
|
|
3869
|
+
This document discusses why \`${provider.pkgName}\` is built the way it is.
|
|
3870
|
+
It does not tell you how to do anything — for that see the
|
|
3871
|
+
[tutorial](tutorial.md) and the [how-to guides](how-to.md), and for the exact
|
|
3872
|
+
patterns, entities and options, the [reference](reference.md). The whole set is
|
|
3873
|
+
indexed in [doc/README.md](README.md).
|
|
3874
|
+
|
|
3875
|
+
|
|
3876
|
+
## The provider convention
|
|
3877
|
+
|
|
3878
|
+
Seneca applications talk to the outside world through *providers*. A provider
|
|
3879
|
+
is a plugin that makes a third-party API look like a Seneca data source, so
|
|
3880
|
+
application code uses the entity API it already knows instead of learning a
|
|
3881
|
+
client library per service.
|
|
3882
|
+
|
|
3883
|
+
The payoff is uniformity. An application reading from ${provider.api}, a
|
|
3884
|
+
payment processor and a CRM uses one access pattern for all three:
|
|
3885
|
+
|
|
3886
|
+
\`\`\`js
|
|
3887
|
+
await seneca.entity('provider/${provider.lower}/${subject.name}').list$()
|
|
3888
|
+
await seneca.entity('provider/stripe/charge').list$()
|
|
3889
|
+
\`\`\`
|
|
3890
|
+
|
|
3891
|
+
Because these are ordinary Seneca entities, everything built on the entity API
|
|
3892
|
+
— logging, tracing, message interception, test doubles — applies to remote
|
|
3893
|
+
calls without any special support for HTTP.
|
|
3894
|
+
|
|
3895
|
+
|
|
3896
|
+
## What entityBuilder buys
|
|
3897
|
+
|
|
3898
|
+
The convention is more than a naming scheme. \`@seneca/provider\` exports
|
|
3899
|
+
\`provider/entityBuilder\`, and this plugin hands it exactly one thing: a map
|
|
3900
|
+
from entity name to a small set of cmd actions. Recognising the
|
|
3901
|
+
\`provider/${provider.lower}/\` canon, registering the \`role:entity\` messages
|
|
3902
|
+
that sit behind \`list$\`, \`load$\`, \`save$\` and \`remove$\`, and turning
|
|
3903
|
+
whatever an action returns into an entity of the right canon — none of that is
|
|
3904
|
+
written here. It arrives with the convention.
|
|
3905
|
+
|
|
3906
|
+
What remains is a handful of async functions, each a few lines long, whose
|
|
3907
|
+
whole job is to call the SDK and hand the result back through the \`entize\`
|
|
3908
|
+
function entityBuilder supplies. That thinness is the point rather than an
|
|
3909
|
+
accident of effort: a provider that is nearly all glue can be read at a glance,
|
|
3910
|
+
generated in full, and regenerated when the API moves. Cleverness added here is
|
|
3911
|
+
cleverness that has to be maintained against a moving target.
|
|
3912
|
+
|
|
3913
|
+
|
|
3914
|
+
## Two layers of the same idea
|
|
3915
|
+
|
|
3916
|
+
This provider is unusual among Seneca providers in that the thing it wraps is
|
|
3917
|
+
*already* entity-shaped. The ${provider.api} SDK exposes accessors like
|
|
3918
|
+
\`client.${subject.acc}()\` — carrying
|
|
3919
|
+
${list(subject.ops)} —
|
|
3920
|
+
rather than raw HTTP routes, for much the same reason Seneca does. A small,
|
|
3921
|
+
uniform surface is easier for people and for agents to reason about than a set
|
|
3922
|
+
of URL templates.
|
|
3923
|
+
|
|
3924
|
+
So the provider is mostly a translation between two entity models that already
|
|
3925
|
+
agree on the important things. Where they *disagree* is where this plugin has
|
|
3926
|
+
to do real work, and each disagreement is discussed below.
|
|
3927
|
+
|
|
3928
|
+
|
|
3929
|
+
## Where the SDK and Seneca disagree
|
|
3930
|
+
|
|
3931
|
+
### Four commands, five operations
|
|
3932
|
+
|
|
3933
|
+
Seneca's store commands are \`list\`, \`load\`, \`save\` and \`remove\`. The
|
|
3934
|
+
SDK's operations are \`list\`, \`load\`, \`create\`, \`update\` and \`remove\`.
|
|
3935
|
+
Four of the five line up. \`save\` is the join, and it dispatches on the id: an
|
|
3936
|
+
entity carrying one is an update, an entity without one is a create.
|
|
3937
|
+
|
|
3938
|
+
`)
|
|
3939
|
+
|
|
3940
|
+
if (0 < writable.length) {
|
|
3941
|
+
Content(`That is Seneca's convention rather than this plugin's invention, and it is a
|
|
3942
|
+
good one. Exposing create and update separately would push the HTTP verb back
|
|
3943
|
+
into application code — the caller who loaded a record, changed a field and
|
|
3944
|
+
called \`save$\` would have to know whether that becomes a POST or a PUT. The
|
|
3945
|
+
presence of the id already answers the question. Asking the caller to answer it
|
|
3946
|
+
again only adds a way to be wrong.
|
|
3947
|
+
|
|
3948
|
+
`)
|
|
3949
|
+
}
|
|
3950
|
+
|
|
3951
|
+
Content(`Which commands exist at all is decided per entity, from the operations the API
|
|
3952
|
+
declares, rather than from an assumption that everything is CRUD.
|
|
3953
|
+
\`${subject.name}\` carries
|
|
3954
|
+
${list(subject.cmds.map((c: string) => c + '$'))}.
|
|
3955
|
+
${othersSentence}
|
|
3956
|
+
An entity whose API has no create and no update simply has no \`save$\`, which
|
|
3957
|
+
is a better answer than a \`save$\` that exists and then fails at the HTTP
|
|
3958
|
+
layer.
|
|
3959
|
+
|
|
3960
|
+
`)
|
|
3961
|
+
|
|
3962
|
+
if (0 < onesided.length) {
|
|
3963
|
+
Content(`Where an entity declares only one of create and update there is nothing to
|
|
3964
|
+
dispatch on, and \`save$\` means that operation whether an id is present or not:
|
|
3965
|
+
|
|
3966
|
+
`)
|
|
3967
|
+
each(onesided, (e: any) => {
|
|
3968
|
+
Content(`- \`${e.name}\`: \`save$\` always ${e.ops.includes('create') ? 'creates' : 'updates'}
|
|
3969
|
+
`)
|
|
3970
|
+
})
|
|
3971
|
+
Content(`
|
|
3972
|
+
`)
|
|
3973
|
+
}
|
|
3974
|
+
|
|
3975
|
+
Content(`### Entity instances versus plain data
|
|
3976
|
+
|
|
3977
|
+
Every SDK operation resolves to an SDK entity instance, never to raw data:
|
|
3978
|
+
\`list\` to a list of them, and each single-record operation to one. The record
|
|
3979
|
+
is absorbed into the instance and read back through \`.data()\`.${0 < removable.length ?
|
|
3980
|
+
` A removed\nentity is the same instance, marked deleted, still holding what it held.` : ''}
|
|
3981
|
+
|
|
3982
|
+
Seneca's \`entize\` wants plain data, so the provider takes the \`.data()\` hop
|
|
3983
|
+
on everything the SDK hands back, before it goes anywhere near an entity. That
|
|
3984
|
+
is the whole of the \`plain\` helper in the source, and it is the only place in
|
|
3985
|
+
the plugin that knows the SDK deals in instances at all.
|
|
3986
|
+
|
|
3987
|
+
The hop earns its keep for a second reason. An SDK instance carries its own
|
|
3988
|
+
serialisation marker, and that marker must not survive into a Seneca entity:
|
|
3989
|
+
Seneca reads \`entity$\` on a data object as the *canon*. A marker landing on
|
|
3990
|
+
that key would be taken as a canon, and the record would come back under the
|
|
3991
|
+
wrong one — or under none. The SDK namespaces its marker so the collision
|
|
3992
|
+
cannot happen by accident, but normalising at this boundary is still the right
|
|
3993
|
+
call. It is what makes the data plain, and it keeps the provider independent of
|
|
3994
|
+
whatever the SDK decides to carry alongside a record.
|
|
3995
|
+
|
|
3996
|
+
`)
|
|
3997
|
+
|
|
3998
|
+
if (0 < loadable.length) {
|
|
3999
|
+
Content(`### Missing things
|
|
4000
|
+
|
|
4001
|
+
\`load$\` for an id that does not exist resolves to \`null\`. Only a 404 is
|
|
4002
|
+
translated this way; every other failure propagates.
|
|
4003
|
+
|
|
4004
|
+
"This thing does not exist" is an ordinary answer to a lookup, not a failure of
|
|
4005
|
+
the lookup. It is usually a branch in the caller's logic, and forcing every call
|
|
4006
|
+
site into a \`try\`/\`catch\` to express that branch makes the common path noisy.
|
|
4007
|
+
A malformed request, a rejected credential or an unreachable server means
|
|
4008
|
+
something else entirely: the question could not be asked, and that should
|
|
4009
|
+
interrupt rather than quietly look like an empty result.
|
|
4010
|
+
|
|
4011
|
+
The SDK does not draw this line — it throws for any non-2xx — so the provider
|
|
4012
|
+
asks the thrown error, which reports \`notFound\` and the HTTP \`status\` at the
|
|
4013
|
+
top level. That coupling to the SDK's error shape is a deliberate and narrow
|
|
4014
|
+
one, and it is why the shape is written down in the
|
|
4015
|
+
[reference](reference.md).
|
|
4016
|
+
|
|
4017
|
+
`)
|
|
4018
|
+
|
|
4019
|
+
if (0 < removable.length) {
|
|
4020
|
+
Content(`\`remove$\` is treated the same way and for the same reason: removing something
|
|
4021
|
+
that is already gone leaves the caller with what the caller wanted.
|
|
4022
|
+
|
|
4023
|
+
`)
|
|
4024
|
+
}
|
|
4025
|
+
}
|
|
4026
|
+
else if (0 < removable.length) {
|
|
4027
|
+
Content(`### Missing things
|
|
4028
|
+
|
|
4029
|
+
Nothing here reads a single record by id, but \`remove$\` still has to decide
|
|
4030
|
+
what "it was not there" means, and it treats a 404 as an ordinary outcome rather
|
|
4031
|
+
than a failure: the record is gone, which is what the caller asked for. Every
|
|
4032
|
+
other failure — a malformed request, a rejected credential, an unreachable
|
|
4033
|
+
server — means the question could not be asked at all, and propagates.
|
|
4034
|
+
|
|
4035
|
+
The SDK does not draw that line; it throws for any non-2xx. So the provider asks
|
|
4036
|
+
the thrown error, which reports \`notFound\` and the HTTP \`status\` at the top
|
|
4037
|
+
level. That coupling to the SDK's error shape is a deliberate and narrow one,
|
|
4038
|
+
and it is why the shape is written down in the [reference](reference.md).
|
|
4039
|
+
|
|
4040
|
+
`)
|
|
4041
|
+
}
|
|
4042
|
+
|
|
4043
|
+
if (0 < nested.length) {
|
|
4044
|
+
Content(`### Nesting
|
|
4045
|
+
|
|
4046
|
+
${nestLead}
|
|
4047
|
+
Seneca's entity model is flat — a canon has no notion of a parent.
|
|
4048
|
+
|
|
4049
|
+
The gap is bridged by putting the parent id in the query, which is why
|
|
4050
|
+
\`${n.parents[0]}\` is required on every \`${n.name}\` command${n.cmds.includes('load') ?
|
|
4051
|
+
`, and why\n\`${n.name}\` \`load$\` takes an object rather than a bare id string` : ''}.
|
|
4052
|
+
This is inherited from the API's URL structure — \`${n.path}\` — rather than
|
|
4053
|
+
chosen here.
|
|
4054
|
+
|
|
4055
|
+
The provider checks for \`${n.parents[0]}\` itself and throws a named error
|
|
4056
|
+
rather than letting the request go out. Without the check, the SDK builds a URL
|
|
4057
|
+
with a missing segment and the server answers 404${n.cmds.includes('load') ?
|
|
4058
|
+
`, and that 404 is\nindistinguishable from "that ${n.name} does not exist" — which the provider\nwould then dutifully translate to \`null\`. A forgotten argument would look\nexactly like an empty result` :
|
|
4059
|
+
` — an opaque failure that says\nnothing about the argument that was left out`}. Failing early turns a confusing
|
|
4060
|
+
wrong answer into an obvious mistake.
|
|
4061
|
+
|
|
4062
|
+
`)
|
|
4063
|
+
if (1 < nested.length) {
|
|
4064
|
+
Content(`The same applies to every nested entity here —
|
|
4065
|
+
${list(nested.map((e: any) => e.name))} — each guarded on its own keys.
|
|
4066
|
+
|
|
4067
|
+
`)
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
|
|
4071
|
+
Content(`### Query directives
|
|
4072
|
+
|
|
4073
|
+
Seneca store queries can carry directives such as \`sort$\` and \`limit$\`.
|
|
4074
|
+
These are instructions to a *store*, and the API has no equivalent, so the
|
|
4075
|
+
provider strips any key ending in \`$\` before the query becomes an API match.
|
|
4076
|
+
|
|
4077
|
+
Passing them through would be worse than dropping them: the SDK would forward
|
|
4078
|
+
them as ordinary match fields, and the API would either ignore them or reject
|
|
4079
|
+
the request outright. Dropping them is imperfect too — a caller who writes
|
|
4080
|
+
\`list$({ sort$: 'name' })\` gets unsorted results and no complaint — but it is
|
|
4081
|
+
the behaviour least likely to produce a wrong answer, and the limitation is
|
|
4082
|
+
documented rather than hidden. Sorting and limiting belong on the caller's
|
|
4083
|
+
side, or in the API's own query fields where it has them.
|
|
4084
|
+
|
|
4085
|
+
|
|
4086
|
+
`)
|
|
4087
|
+
|
|
4088
|
+
if (0 < writable.length) {
|
|
4089
|
+
Content(`## Why writes are supported here
|
|
4090
|
+
|
|
4091
|
+
The read-only question is worth asking of every provider, and the answer here
|
|
4092
|
+
follows from the API rather than from taste.
|
|
4093
|
+
|
|
4094
|
+
Writes map cleanly onto entities only when the API's notion of "save" is
|
|
4095
|
+
unambiguous. For a CMS with draft states, localised fields and a separate
|
|
4096
|
+
publish step, \`save$\` would have to pick one interpretation and would mislead
|
|
4097
|
+
whoever guessed differently. Here the write operations are plain whole-record
|
|
4098
|
+
ones, so \`save$\` can mean exactly one thing for each of
|
|
4099
|
+
${list(writable.map((e: any) => e.name))}, and the store surface those
|
|
4100
|
+
operations support is implemented in full.
|
|
4101
|
+
|
|
4102
|
+
One wrinkle does not map cleanly. Seneca's model lets a caller choose an id;
|
|
4103
|
+
many APIs assign ids themselves and ignore any id sent on create. The provider
|
|
4104
|
+
does not try to paper over that, because it cannot make a server honour an id
|
|
4105
|
+
it did not issue. Code that predicts the id of a record it is about to create
|
|
4106
|
+
will be wrong on such an API, and the remedy is to read the id back from what
|
|
4107
|
+
\`save$\` returns rather than to guess it beforehand.
|
|
4108
|
+
|
|
4109
|
+
|
|
4110
|
+
`)
|
|
4111
|
+
}
|
|
4112
|
+
else {
|
|
4113
|
+
Content(`## Why this provider only reads
|
|
4114
|
+
|
|
4115
|
+
Every entity here exposes reads alone. That is not a policy decision taken in
|
|
4116
|
+
the plugin: the cmd map is built from the operations the API declares, and none
|
|
4117
|
+
of these entities declares a create or an update. A \`save$\` that existed only
|
|
4118
|
+
to fail at the HTTP layer would be worse than no \`save$\` at all — the absence
|
|
4119
|
+
is the honest signal, and it appears in the entity table in the
|
|
4120
|
+
[reference](reference.md).
|
|
4121
|
+
|
|
4122
|
+
If the API grows write operations, they arrive here by regeneration rather than
|
|
4123
|
+
by hand. Nothing about the mapping is waiting to be written.
|
|
4124
|
+
|
|
4125
|
+
|
|
4126
|
+
`)
|
|
4127
|
+
}
|
|
4128
|
+
|
|
4129
|
+
Content(`## Credentials, whether or not the API needs them
|
|
4130
|
+
|
|
4131
|
+
At startup the plugin asks \`@seneca/provider\` for the keymap of
|
|
4132
|
+
\`${provider.lower}\` and sends the \`apikey\` as a bearer token when one is
|
|
4133
|
+
configured.
|
|
4134
|
+
|
|
4135
|
+
The key is *optional*. Absent, unconfigured and empty all mean "send no
|
|
4136
|
+
header", and none of them is an error. For an API that needs no credential this
|
|
4137
|
+
looks like ceremony, and it is worth keeping anyway: the shape of a Seneca
|
|
4138
|
+
application should not depend on whether a particular service happens to need a
|
|
4139
|
+
key. An application that moves from an open endpoint to an authenticated
|
|
4140
|
+
deployment then changes one configuration value rather than restructuring how
|
|
4141
|
+
the plugin loads — and a provider that demanded a key from an API that has none
|
|
4142
|
+
would force every user to invent a fake one.
|
|
4143
|
+
|
|
4144
|
+
|
|
4145
|
+
## Depending on a published SDK
|
|
4146
|
+
|
|
4147
|
+
The SDK is an ordinary published dependency: \`${provider.sdkPkg}\` at
|
|
4148
|
+
\`^${provider.sdkVersion}\`, resolved by npm like anything else.
|
|
4149
|
+
|
|
4150
|
+
The alternative is vendoring — copying the generated client into this
|
|
4151
|
+
repository. That is tempting, since both artefacts come from the same model and
|
|
4152
|
+
change together. It is also wrong. It makes a second copy of something that is
|
|
4153
|
+
regenerated whenever the API moves, and it puts this plugin's release cycle in
|
|
4154
|
+
charge of the API's. As a dependency, the SDK carries its own semantic version:
|
|
4155
|
+
when the API changes, the SDK is versioned, and this plugin either follows the
|
|
4156
|
+
range or pins until it is ready. Keeping them separable also matters to the
|
|
4157
|
+
people who use the SDK with no Seneca anywhere in sight.
|
|
4158
|
+
|
|
4159
|
+
One consequence of depending on generated code is worth stating plainly. The
|
|
4160
|
+
SDK is regenerated as the API model changes, so its surface can shift in ways a
|
|
4161
|
+
hand-written library's would not. That argues for keeping this plugin thin, and
|
|
4162
|
+
for pinning behaviour in tests. Everything this plugin knows about the SDK's
|
|
4163
|
+
shapes is concentrated in three small functions — the \`.data()\` hop, the query
|
|
4164
|
+
cleaner and the not-found translation — plus the construction of the client, so
|
|
4165
|
+
an SDK change is absorbed in one place and surfaces as a failing offline test
|
|
4166
|
+
rather than as a surprise in production.
|
|
4167
|
+
|
|
4168
|
+
`)
|
|
4169
|
+
|
|
4170
|
+
if ('' === provider.liveBase) {
|
|
4171
|
+
Content(`The API definition declares no server, so this plugin has no default host: the
|
|
4172
|
+
base URL arrives through the \`sdk.base\` option, supplied by whoever configures
|
|
4173
|
+
the plugin for a particular deployment. The tests therefore run entirely
|
|
4174
|
+
against the SDK's mock transport, which is the one host that is always
|
|
4175
|
+
available.
|
|
4176
|
+
|
|
4177
|
+
|
|
4178
|
+
`)
|
|
4179
|
+
}
|
|
4180
|
+
else {
|
|
4181
|
+
Content(`The distinction that does survive is between the SDK and its **test server**.
|
|
4182
|
+
The SDK is published; the server is not, and ships only in
|
|
4183
|
+
[the SDK's source repository](${provider.sdkRepoUrl}). So the offline tests need
|
|
4184
|
+
nothing but \`npm install\`, while the live tests need a clone. That asymmetry
|
|
4185
|
+
is why the live tests probe for the server and skip rather than fail: the common
|
|
4186
|
+
case is a contributor who has the dependency but not the repository.
|
|
4187
|
+
|
|
4188
|
+
|
|
4189
|
+
`)
|
|
4190
|
+
}
|
|
4191
|
+
|
|
4192
|
+
Content(`## A generated plugin
|
|
4193
|
+
|
|
4194
|
+
Nothing in this repository is hand-written. The plugin source, its tests, its CI
|
|
4195
|
+
workflow, its manifest and these documents are all emitted by
|
|
4196
|
+
[@voxgig/sdkgen](https://github.com/voxgig/sdkgen) from the ${provider.api} API
|
|
4197
|
+
model — the same model the SDK is generated from, which is why the two cannot
|
|
4198
|
+
disagree about entity names, id fields, or which operations exist.
|
|
4199
|
+
|
|
4200
|
+
There is one blunt consequence for anyone reading the code and reaching for an
|
|
4201
|
+
edit: the edit will not survive. The next generation run overwrites this
|
|
4202
|
+
repository, without a merge and without a warning. A fix applied here is a fix
|
|
4203
|
+
that has to be applied again, silently, forever.
|
|
4204
|
+
|
|
4205
|
+
The source of truth is the SDK project's model — \`${provider.sdkrel}\` from
|
|
4206
|
+
here, if both are checked out — together with the sdkgen component that emits
|
|
4207
|
+
this target. A change to *what* the API offers belongs in the model; a change to
|
|
4208
|
+
*how* the provider expresses it belongs in the component. Both are versioned,
|
|
4209
|
+
both regenerate every provider built this way rather than just this one, and
|
|
4210
|
+
both are where a fix is worth making. See
|
|
4211
|
+
[Contributing](../README.md#contributing).
|
|
4212
|
+
|
|
4213
|
+
|
|
4214
|
+
## How the tests are arranged
|
|
4215
|
+
|
|
4216
|
+
The suite runs offline by default. It needs no credentials and no network.
|
|
4217
|
+
|
|
4218
|
+
The **offline** tests use the SDK's own mock transport, reached through this
|
|
4219
|
+
plugin's own options:
|
|
4220
|
+
|
|
4221
|
+
\`\`\`js
|
|
4222
|
+
.use('${provider.pkgName}', {
|
|
4223
|
+
test: true,
|
|
4224
|
+
testopts: { entity: { ${subject.name}: { '${subject.name}0': { ... } } } },
|
|
4225
|
+
})
|
|
4226
|
+
\`\`\`
|
|
4227
|
+
|
|
4228
|
+
This is better than the usual provider-testing compromise. Rather than checking
|
|
4229
|
+
only that the plugin loads and answers
|
|
4230
|
+
\`sys:provider,provider:${provider.lower},get:info\`, the tests exercise the
|
|
4231
|
+
entity commands themselves${coveredPhrase}
|
|
4232
|
+
through the real code path, from a Seneca entity call down to the transport and
|
|
4233
|
+
back. The only thing replaced is the socket. And because the mock belongs to the
|
|
4234
|
+
SDK, it stays honest as the SDK changes: a regeneration that alters a return
|
|
4235
|
+
shape breaks a test here rather than someone's production run.
|
|
4236
|
+
|
|
4237
|
+
Seeding the mock is not decoration either. The seed is generated from the same
|
|
4238
|
+
model as the entities, so the records the tests read carry the fields the API
|
|
4239
|
+
would really return${0 < nested.length ? `, and a nested record's parent id
|
|
4240
|
+
names a parent record that exists — otherwise the nested tests would read an
|
|
4241
|
+
empty store and pass without proving anything` : ''}.
|
|
4242
|
+
|
|
4243
|
+
`)
|
|
4244
|
+
|
|
4245
|
+
if ('' !== provider.liveBase) {
|
|
4246
|
+
Content(`The **live** tests point at the companion server in the SDK repository and probe
|
|
4247
|
+
it before running, skipping with a stated reason when nothing answers. So a
|
|
4248
|
+
contributor who has just cloned this repository gets a meaningful result
|
|
4249
|
+
immediately, and a more thorough one after starting the server.
|
|
4250
|
+
|
|
4251
|
+
Skipping is deliberate, and preferred over quietly returning early. An early
|
|
4252
|
+
\`return\` reports a test as *passed*, which makes an unconfigured checkout look
|
|
4253
|
+
as though it verified the integration when it verified nothing at all. A skip is
|
|
4254
|
+
honest about coverage, and the summary count shows how much did not run.
|
|
4255
|
+
|
|
4256
|
+
`)
|
|
4257
|
+
|
|
4258
|
+
if (subject.cmds.includes('save') && subject.cmds.includes('remove')) {
|
|
4259
|
+
Content(`The manual scripts in \`test/\` that write to a live server remove what they
|
|
4260
|
+
create, in a \`finally\` block, so the server is left as it was found. A run that
|
|
4261
|
+
leaks a record changes the result of the next one, which is how a suite becomes
|
|
4262
|
+
order-dependent and then flaky.
|
|
4263
|
+
`)
|
|
4264
|
+
}
|
|
4265
|
+
}
|
|
4266
|
+
})
|
|
4267
|
+
})
|
|
4268
|
+
|
|
4269
|
+
|
|
4270
|
+
// --- doc/ --------------------------------------------------------------------
|
|
4271
|
+
//
|
|
4272
|
+
// The Diátaxis documentation set: an index plus the four quadrants.
|
|
4273
|
+
//
|
|
4274
|
+
// WHY THIS IS GENERATED AT ALL. Every other sdkgen target emits a single
|
|
4275
|
+
// README, and for a language SDK that is the right amount: the SDK's real
|
|
4276
|
+
// reference is its types. A Seneca provider has no types a reader can browse —
|
|
4277
|
+
// its whole interface is message patterns and entity canons, which exist only
|
|
4278
|
+
// in prose. The provider this target was modelled on carried 1100 lines of
|
|
4279
|
+
// hand-written documentation for exactly that reason, and the first
|
|
4280
|
+
// regeneration left all of it orphaned: the README's link table was gone and
|
|
4281
|
+
// nothing emitted the files it had pointed at.
|
|
4282
|
+
//
|
|
4283
|
+
// Everything here is derived from the same `provider` shape the source and the
|
|
4284
|
+
// tests are built from, so the docs cannot describe an entity the plugin does
|
|
4285
|
+
// not expose, or a cmd it does not implement — the drift that makes
|
|
4286
|
+
// hand-written provider docs untrustworthy after the second API change.
|
|
4287
|
+
|
|
4288
|
+
const DocIndex = cmp(function DocIndex(props: any) {
|
|
4289
|
+
const { provider } = props
|
|
4290
|
+
|
|
4291
|
+
File({ name: 'README.md' }, () => {
|
|
4292
|
+
Content(`# Documentation
|
|
4293
|
+
|
|
4294
|
+
The documentation for \`${provider.pkgName}\` follows the
|
|
4295
|
+
[Diátaxis](https://diataxis.fr) framework. Each document serves one purpose,
|
|
4296
|
+
and that purpose decides what belongs in it. If you are unsure where to look,
|
|
4297
|
+
use the table below.
|
|
4298
|
+
|
|
4299
|
+
| Document | Purpose | Read it when |
|
|
4300
|
+
| -------- | ------- | ------------ |
|
|
4301
|
+
| [Tutorial](tutorial.md) | Learning-oriented. A lesson that takes you from nothing to a working script. | You have never used this plugin and want to see it work. |
|
|
4302
|
+
| [How-to guides](how-to.md) | Task-oriented. Recipes that solve one problem each. | You know what you want to do and need the steps. |
|
|
4303
|
+
| [Reference](reference.md) | Information-oriented. A complete, factual description of the interface. | You need to look up a message pattern, entity field, or option. |
|
|
4304
|
+
| [Explanation](explanation.md) | Understanding-oriented. The reasoning behind the design. | You want to know *why* it works this way, or you are debugging something surprising. |
|
|
4305
|
+
|
|
4306
|
+
## The distinction that matters most
|
|
4307
|
+
|
|
4308
|
+
The tutorial and the how-to guides look alike — both are sequences of steps —
|
|
4309
|
+
but they are not interchangeable.
|
|
4310
|
+
|
|
4311
|
+
The **tutorial** is a lesson. It is safe to follow, it produces a result you
|
|
4312
|
+
can see, and it asks you to make no decisions. Its job is to build confidence,
|
|
4313
|
+
so it deliberately avoids alternatives and edge cases.
|
|
4314
|
+
|
|
4315
|
+
A **how-to guide** assumes competence. It answers "how do I list every record
|
|
4316
|
+
of a collection?", and it assumes you already have a working Seneca instance.
|
|
4317
|
+
Its job is to get a task done, so it omits the explanation.
|
|
4318
|
+
|
|
4319
|
+
Likewise **reference** describes the machinery and nothing else — it never
|
|
4320
|
+
teaches. **Explanation** discusses and gives context — it never instructs.
|
|
4321
|
+
|
|
4322
|
+
## These documents are generated
|
|
4323
|
+
|
|
4324
|
+
This plugin, and this documentation with it, is generated by
|
|
4325
|
+
[@voxgig/sdkgen](https://github.com/voxgig/sdkgen) from the ${provider.api} API
|
|
4326
|
+
definition held in the [SDK project](${provider.sdkRepoUrl}). An edit made here
|
|
4327
|
+
is lost on the next regeneration.
|
|
4328
|
+
|
|
4329
|
+
Something genuinely specific to this API — a quirk of its authentication, a
|
|
4330
|
+
rate limit worth warning about — belongs in the model the generator reads, not
|
|
4331
|
+
in the output it writes. Everything else belongs in the generator's own
|
|
4332
|
+
components, where fixing it once fixes every provider.
|
|
4333
|
+
`)
|
|
4334
|
+
})
|
|
4335
|
+
})
|
|
4336
|
+
|
|
4337
|
+
|
|
4338
|
+
// The whole `doc/` folder. One cmp so Main names the documentation once, and
|
|
4339
|
+
// so the folder is opened in a single place — the four quadrant components
|
|
4340
|
+
// emit a File each and know nothing about where they sit.
|
|
4341
|
+
const Docs = cmp(function Docs(props: any) {
|
|
4342
|
+
const { provider } = props
|
|
4343
|
+
|
|
4344
|
+
Folder({ name: 'doc' }, () => {
|
|
4345
|
+
DocIndex({ provider })
|
|
4346
|
+
DocTutorial({ provider })
|
|
4347
|
+
DocHowto({ provider })
|
|
4348
|
+
DocReference({ provider })
|
|
4349
|
+
DocExplanation({ provider })
|
|
4350
|
+
})
|
|
4351
|
+
})
|
|
4352
|
+
|
|
4353
|
+
|
|
4354
|
+
export {
|
|
4355
|
+
Tests,
|
|
4356
|
+
Scripts,
|
|
4357
|
+
Workflow,
|
|
4358
|
+
Readme,
|
|
4359
|
+
Docs,
|
|
4360
|
+
seedRecord,
|
|
4361
|
+
parentSeed,
|
|
4362
|
+
}
|