@openreachtech/hora-skills-ort-renchan 0.0.0
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/LICENSE +201 -0
- package/README.ja.md +135 -0
- package/README.md +135 -0
- package/dist/skills/hor-agent-loop/SKILL.md +123 -0
- package/dist/skills/hor-agent-loop/references/core.md +485 -0
- package/dist/skills/hor-agent-loop/references/graphql-integration.md +178 -0
- package/dist/skills/hor-agent-loop/references/renchan-job-integration.md +368 -0
- package/dist/skills/hor-agent-loop/references/testing.md +492 -0
- package/dist/skills/hor-ai-agent-structure/SKILL.md +403 -0
- package/dist/skills/hor-ai-agent-structure/references/loop-and-actions.md +100 -0
- package/dist/skills/hor-ai-prompt-document-store/SKILL.md +220 -0
- package/dist/skills/hor-ai-prompt-document-store/references/models-and-composition.md +156 -0
- package/dist/skills/hor-backend-testing/SKILL.md +118 -0
- package/dist/skills/hor-backend-testing/references/placement-and-ordering.md +89 -0
- package/dist/skills/hor-backend-testing/references/purity-and-mocks.md +82 -0
- package/dist/skills/hor-backend-testing/references/running-tests.md +186 -0
- package/dist/skills/hor-bank-id/SKILL.md +91 -0
- package/dist/skills/hor-build-e2e-test-environment/SKILL.md +609 -0
- package/dist/skills/hor-build-e2e-test-environment/references/compose-definition.md +368 -0
- package/dist/skills/hor-build-e2e-test-environment/references/edge-and-proxy.md +287 -0
- package/dist/skills/hor-build-e2e-test-environment/references/environment-and-ports.md +201 -0
- package/dist/skills/hor-build-e2e-test-environment/references/runner-and-lifecycle.md +330 -0
- package/dist/skills/hor-build-e2e-test-environment/references/seed-data.md +145 -0
- package/dist/skills/hor-build-e2e-test-environment/references/windows-runner.md +146 -0
- package/dist/skills/hor-constant-definition/SKILL.md +139 -0
- package/dist/skills/hor-cookie-authentication/SKILL.md +33 -0
- package/dist/skills/hor-cookie-authentication/references/auth-filter.md +55 -0
- package/dist/skills/hor-cookie-authentication/references/cookie-context.md +53 -0
- package/dist/skills/hor-cookie-authentication/references/dev-login-seeder.md +22 -0
- package/dist/skills/hor-cookie-authentication/references/environment.md +64 -0
- package/dist/skills/hor-cookie-authentication/references/error-codes.md +24 -0
- package/dist/skills/hor-cookie-authentication/references/migrations.md +18 -0
- package/dist/skills/hor-cookie-authentication/references/resolvers.md +97 -0
- package/dist/skills/hor-cookie-authentication/references/session-clerk.md +41 -0
- package/dist/skills/hor-cookie-authentication/references/token-models.md +125 -0
- package/dist/skills/hor-database-design/SKILL.md +54 -0
- package/dist/skills/hor-database-design/references/column-types.md +92 -0
- package/dist/skills/hor-database-design/references/datetimes.md +88 -0
- package/dist/skills/hor-database-design/references/master-tables.md +117 -0
- package/dist/skills/hor-database-design/references/normalization.md +40 -0
- package/dist/skills/hor-database-design/references/update-history.md +35 -0
- package/dist/skills/hor-execution-placement-pattern/SKILL.md +216 -0
- package/dist/skills/hor-external-api-client/SKILL.md +99 -0
- package/dist/skills/hor-external-api-client/references/architecture.md +127 -0
- package/dist/skills/hor-external-api-client/references/authorization.md +105 -0
- package/dist/skills/hor-external-api-client/references/capsule.md +130 -0
- package/dist/skills/hor-external-api-client/references/conventions.md +68 -0
- package/dist/skills/hor-external-api-client/references/launcher.md +138 -0
- package/dist/skills/hor-external-api-client/references/payload.md +176 -0
- package/dist/skills/hor-external-api-client/references/sdk-wrapper.md +291 -0
- package/dist/skills/hor-external-api-client/references/testing.md +130 -0
- package/dist/skills/hor-external-api-client/references/usage.md +152 -0
- package/dist/skills/hor-graphql-schema/SKILL.md +360 -0
- package/dist/skills/hor-graphql-server-engine/SKILL.md +356 -0
- package/dist/skills/hor-light-rag/SKILL.md +242 -0
- package/dist/skills/hor-light-rag/references/retrieval-internals.md +143 -0
- package/dist/skills/hor-multi-llm-provider/SKILL.md +288 -0
- package/dist/skills/hor-multi-llm-provider/references/provider-internals.md +153 -0
- package/dist/skills/hor-mutation-resolver/SKILL.md +360 -0
- package/dist/skills/hor-mutation-resolver/references/dependency-injection.md +148 -0
- package/dist/skills/hor-mutation-resolver/references/errors.md +120 -0
- package/dist/skills/hor-mutation-resolver/references/resolve-flow.md +275 -0
- package/dist/skills/hor-mutation-resolver/references/testing.md +125 -0
- package/dist/skills/hor-mutation-resolver/references/transaction.md +197 -0
- package/dist/skills/hor-post-worker/SKILL.md +320 -0
- package/dist/skills/hor-query-resolver/SKILL.md +246 -0
- package/dist/skills/hor-query-resolver/references/anatomy.md +384 -0
- package/dist/skills/hor-query-resolver/references/data-access.md +248 -0
- package/dist/skills/hor-query-resolver/references/errors-and-context.md +172 -0
- package/dist/skills/hor-query-resolver/references/testing.md +241 -0
- package/dist/skills/hor-renchan-job-bullmq/SKILL.md +178 -0
- package/dist/skills/hor-renchan-job-bullmq/references/daemon-and-scheduler.md +182 -0
- package/dist/skills/hor-renchan-job-bullmq/references/engine-and-infra.md +106 -0
- package/dist/skills/hor-renchan-job-bullmq/references/job-triple.md +189 -0
- package/dist/skills/hor-renchan-job-bullmq/references/queues.md +74 -0
- package/dist/skills/hor-renchan-job-bullmq/references/subscriptions.md +113 -0
- package/dist/skills/hor-resolver-share/SKILL.md +262 -0
- package/dist/skills/hor-resolver-validator/SKILL.md +194 -0
- package/dist/skills/hor-resolver-validator/references/inspector-api.md +115 -0
- package/dist/skills/hor-resolver-validator/references/validator-pattern.md +281 -0
- package/dist/skills/hor-restfulapi-architecture/SKILL.md +275 -0
- package/dist/skills/hor-restfulapi-architecture/references/engine-and-context.md +192 -0
- package/dist/skills/hor-restfulapi-architecture/references/renderers.md +204 -0
- package/dist/skills/hor-restfulapi-architecture/references/response-and-flusher.md +118 -0
- package/dist/skills/hor-restfulapi-architecture/references/validation.md +229 -0
- package/dist/skills/hor-security-audit/SKILL.md +110 -0
- package/dist/skills/hor-security-audit/references/application-surface.md +90 -0
- package/dist/skills/hor-security-audit/references/auth-and-transport.md +147 -0
- package/dist/skills/hor-security-audit/references/code-safety.md +74 -0
- package/dist/skills/hor-security-audit/references/dependencies.md +72 -0
- package/dist/skills/hor-security-audit/references/network-and-logging.md +56 -0
- package/dist/skills/hor-security-audit/references/secrets.md +95 -0
- package/dist/skills/hor-sequelize-migration/SKILL.md +336 -0
- package/dist/skills/hor-sequelize-migration/references/alter-table.md +147 -0
- package/dist/skills/hor-sequelize-migration/references/foreign-keys.md +107 -0
- package/dist/skills/hor-sequelize-migration/references/indexes.md +149 -0
- package/dist/skills/hor-sequelize-migration/references/notation.md +215 -0
- package/dist/skills/hor-sequelize-model/SKILL.md +69 -0
- package/dist/skills/hor-sequelize-model/references/associations.md +218 -0
- package/dist/skills/hor-sequelize-model/references/default-methods.md +244 -0
- package/dist/skills/hor-sequelize-model/references/mixins.md +247 -0
- package/dist/skills/hor-sequelize-model/references/notation.md +225 -0
- package/dist/skills/hor-sequelize-model/references/timestamps.md +74 -0
- package/dist/skills/hor-sequelize-seeder/SKILL.md +275 -0
- package/dist/skills/hor-sequelize-seeder/references/development-coverage.md +74 -0
- package/dist/skills/hor-sequelize-seeder/references/directory-structure.md +113 -0
- package/dist/skills/hor-sequelize-seeder/references/id-numbering.md +133 -0
- package/dist/skills/hor-sequelize-seeder/references/naming.md +59 -0
- package/dist/skills/hor-sequelize-seeder/references/notation.md +170 -0
- package/dist/skills/hor-sequelize-subquery/SKILL.md +369 -0
- package/dist/skills/hor-strategy-pattern/SKILL.md +371 -0
- package/dist/skills/hor-stub-api/SKILL.md +330 -0
- package/dist/skills/hor-subscription-resolver/SKILL.md +105 -0
- package/dist/skills/hor-subscription-resolver/references/authentication.md +99 -0
- package/dist/skills/hor-subscription-resolver/references/basic-notation.md +116 -0
- package/dist/skills/hor-subscription-resolver/references/design-philosophy.md +68 -0
- package/dist/skills/hor-type-interface/SKILL.md +200 -0
- package/docs/skills.ja.md +40 -0
- package/docs/skills.md +40 -0
- package/lib/equip/CommandLineArguments.js +178 -0
- package/lib/equip/HoraSkillsCli.js +590 -0
- package/lib/equip/SkillsInstaller.js +443 -0
- package/lib/equip/SkillsManifestFile.js +260 -0
- package/lib/equip/constants/CLI_COMMAND.js +8 -0
- package/lib/equip/hora-skills-ort-renchan.js +8 -0
- package/package.json +51 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Capsule (the response wrapper, extracting values)
|
|
2
|
+
|
|
3
|
+
How to write a capsule that extends `BaseCapsule`. It is what `launchRequest()` returns, and
|
|
4
|
+
**differences in response shape — key names, nesting, missing fields — are confined here**
|
|
5
|
+
([SKILL.md](../SKILL.md#first-principle-confine-every-difference-from-the-external-api-to-payload--capsule-anti-corruption-layer)).
|
|
6
|
+
The input side is in [payload.md](./payload.md). The capsule for the SDK-wrapping pattern is in
|
|
7
|
+
[sdk-wrapper.md](./sdk-wrapper.md).
|
|
8
|
+
|
|
9
|
+
## The main members `BaseCapsule` provides
|
|
10
|
+
|
|
11
|
+
| Member | Role |
|
|
12
|
+
| :-- | :-- |
|
|
13
|
+
| `get body` | The normalized response body (after `bodySchema` / `ResponseBodyCtor`). `null` when there is none |
|
|
14
|
+
| `get statusCode` | The HTTP status code. `null` when there is none |
|
|
15
|
+
| `hasError()` | `true` on any error: authentication, input, network, parse, or status (>= 400) |
|
|
16
|
+
| `getErrorMessage()` | The error-code string for the error kind. `null` when there is no error |
|
|
17
|
+
| `isPending()` | `true` before sending (when `payload` is `null`) |
|
|
18
|
+
|
|
19
|
+
- The caller **decides failure with `hasError()`** and writes no `try-catch`
|
|
20
|
+
([SKILL.md](../SKILL.md#decide-failure-with-capsulehaserror-not-try-catch)).
|
|
21
|
+
|
|
22
|
+
## When the response is snake_case, swap `ResponseBodyCtor` for the Camel one
|
|
23
|
+
|
|
24
|
+
Returning `CamelCasedKeyResponseBody` from `static get ResponseBodyCtor ()` converts incoming
|
|
25
|
+
snake_case keys into internal camelCase. `this.body` is then read with the converted keys.
|
|
26
|
+
|
|
27
|
+
- **Why**: even when the external API returns `document_id`, the application stays consistent with
|
|
28
|
+
`documentId`. Confining the incoming naming difference to the capsule lets everything from
|
|
29
|
+
`extractXxx()` onward be written in our own conventions only.
|
|
30
|
+
|
|
31
|
+
## `extractXxx()` always guards against null, and returns `null` / an empty array when the value is missing
|
|
32
|
+
|
|
33
|
+
Add one `extractXxx()` per value you want out of the response.
|
|
34
|
+
|
|
35
|
+
- `this.body` **can be `null`** (on a network or parse failure, for instance). **Always guard with
|
|
36
|
+
optional chaining** or equivalent before returning a value.
|
|
37
|
+
- When the value cannot be produced, return **`null`** — or an **empty array** for something that
|
|
38
|
+
returns an array — never `undefined`
|
|
39
|
+
([SKILL.md](../SKILL.md#return-null-or-an-empty-array-for-a-missing-value-never-undefined)).
|
|
40
|
+
- **Why the null guard is required**: forget it and an error `Capsule` (whose `body` is `null`) throws a
|
|
41
|
+
`TypeError` at `this.body.documentId`, which breaks the design of deciding failure with
|
|
42
|
+
`hasError()`. Extraction guarantees a safe default even when the request failed.
|
|
43
|
+
- **Why not `undefined`**: `undefined` cannot be told apart from "undefined behaviour / not implemented
|
|
44
|
+
yet". `null` (or an empty array) states the intent to the caller: the value was looked for and was
|
|
45
|
+
not there.
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import {
|
|
49
|
+
BaseCapsule,
|
|
50
|
+
CamelCasedKeyResponseBody,
|
|
51
|
+
} from '@openreachtech/mentsu-rocket-client'
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @extends {BaseCapsule<*, *>}
|
|
55
|
+
*/
|
|
56
|
+
export default class CreateDocumentCapsule extends BaseCapsule {
|
|
57
|
+
/**
|
|
58
|
+
* get: ResponseBody constructor (snake_case -> camelCase).
|
|
59
|
+
*
|
|
60
|
+
* @override
|
|
61
|
+
* @returns {typeof CamelCasedKeyResponseBody}
|
|
62
|
+
*/
|
|
63
|
+
static get ResponseBodyCtor () {
|
|
64
|
+
return CamelCasedKeyResponseBody
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Extract created document ID.
|
|
69
|
+
*
|
|
70
|
+
* @returns {string | null}
|
|
71
|
+
*/
|
|
72
|
+
extractDocumentId () {
|
|
73
|
+
return this.body?.documentId
|
|
74
|
+
?? null
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
// Bad: no null guard, and it returns undefined
|
|
81
|
+
extractDocumentId () {
|
|
82
|
+
return this.body.documentId // TypeError when body is null (breaks on an error Capsule)
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Extracting an array — an empty array when missing, reshaped to our own keys:
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
import {
|
|
90
|
+
BaseCapsule,
|
|
91
|
+
CamelCasedKeyResponseBody,
|
|
92
|
+
} from '@openreachtech/mentsu-rocket-client'
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @extends {BaseCapsule<*, *>}
|
|
96
|
+
*/
|
|
97
|
+
export default class FindDocumentsCapsule extends BaseCapsule {
|
|
98
|
+
/** @override */
|
|
99
|
+
static get ResponseBodyCtor () {
|
|
100
|
+
return CamelCasedKeyResponseBody
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Extract documents.
|
|
105
|
+
*
|
|
106
|
+
* @returns {Array<{
|
|
107
|
+
* documentId: string
|
|
108
|
+
* title: string
|
|
109
|
+
* }>}
|
|
110
|
+
*/
|
|
111
|
+
extractDocuments () {
|
|
112
|
+
return this.body?.documents
|
|
113
|
+
?.map(document => ({
|
|
114
|
+
documentId: document.documentId,
|
|
115
|
+
title: document.title,
|
|
116
|
+
}))
|
|
117
|
+
?? []
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
- The `map` **reshapes to only the keys the application needs**. Extra keys and nesting the external
|
|
123
|
+
API returns are not passed to the caller unchanged — that is the confinement. Iterate with `map`,
|
|
124
|
+
not `for` or `forEach` ([conventions.md](./conventions.md)).
|
|
125
|
+
|
|
126
|
+
## Normalize response values with `bodySchema` (optional)
|
|
127
|
+
|
|
128
|
+
Declaring `static bodySchema` makes the declared keys subject to value normalization (normalizing a
|
|
129
|
+
datetime string, say). Use it when you want the values shaped into something the application handles
|
|
130
|
+
more easily. `extractXxx()` works whether or not it is declared.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Conventions (the naming and coding checklist)
|
|
2
|
+
|
|
3
|
+
The naming and coding conventions for writing an external API client: the repository-wide coding
|
|
4
|
+
conventions plus the items specific to this module. Referenced from [SKILL.md](../SKILL.md). The
|
|
5
|
+
cross-cutting rules that matter most — `fetch` only, decide failure with `hasError()`, return `null`,
|
|
6
|
+
comments in English — are in [SKILL.md](../SKILL.md).
|
|
7
|
+
|
|
8
|
+
## Class layout and naming
|
|
9
|
+
|
|
10
|
+
- **One class per file**, with a single `export default`.
|
|
11
|
+
- Class names are Upper CamelCase. Name a derived launcher `<Operation>Launcher`, and the payload and
|
|
12
|
+
capsule `<Operation>Payload` / `<Operation>Capsule`, so **the operation is readable from the name**.
|
|
13
|
+
The base launcher is `Base<Service>Launcher`.
|
|
14
|
+
- **Why include the operation**: one service carries several operations, and without names like
|
|
15
|
+
`CreateDocumentLauncher` / `FindDocumentsLauncher` there is no telling which file is which API.
|
|
16
|
+
|
|
17
|
+
## Method names are "verb + object", and retrieval verbs differ by source
|
|
18
|
+
|
|
19
|
+
A retrieval method's prefix distinguishes where the value comes from.
|
|
20
|
+
|
|
21
|
+
| Prefix | Source |
|
|
22
|
+
| :-- | :-- |
|
|
23
|
+
| `fetch~` | Retrieved from the external API |
|
|
24
|
+
| `find~` | Retrieved from the database |
|
|
25
|
+
| `extract~` | Pulled out of a capsule ([capsule.md](./capsule.md)) |
|
|
26
|
+
|
|
27
|
+
- **Why distinguish them**: retrieval is not one thing — failure means something different, and is
|
|
28
|
+
handled differently, depending on whether the source is the external API, the database or a capsule.
|
|
29
|
+
A consistent prefix makes the kind of I/O readable from the method name alone, and makes it plain at a
|
|
30
|
+
glance that `extract~` performs no external communication.
|
|
31
|
+
|
|
32
|
+
## A constructor only assigns; defaults belong in the factory
|
|
33
|
+
|
|
34
|
+
- A `constructor` **only assigns** the values it was given. No default-value construction, no logic.
|
|
35
|
+
- Defaults belong in `create()` (the factory method), and a getter only returns the field.
|
|
36
|
+
- **Why**: collecting defaults in the factory is what lets a test swap a dependency in, as in
|
|
37
|
+
`create({ sdkClient })` ([sdk-wrapper.md](./sdk-wrapper.md) /
|
|
38
|
+
[usage.md](./usage.md#di-inject-a-stub-launcher)). Logic in the `constructor` removes that seam.
|
|
39
|
+
|
|
40
|
+
## Control flow and data conversion
|
|
41
|
+
|
|
42
|
+
- **No single-character variables** — use a meaningful name (`document`, `functionCall`).
|
|
43
|
+
- Avoid `for` / `forEach` / `switch` / `else if`; write **`map` / `filter` / `reduce` and early
|
|
44
|
+
returns**. Reshape an array in `extractXxx()` with `map`
|
|
45
|
+
([capsule.md](./capsule.md#extractxxx-always-guards-against-null-and-returns-null--an-empty-array-when-the-value-is-missing)).
|
|
46
|
+
- **Why**: a declarative conversion (`map`/`filter`) makes the input-to-output correspondence easy to
|
|
47
|
+
read, and holding no intermediate state leaves less room for bugs. Readability is everything in code
|
|
48
|
+
that absorbs differences, so avoid imperative loops.
|
|
49
|
+
|
|
50
|
+
## Notation
|
|
51
|
+
|
|
52
|
+
- **One property per line in an object literal** — the sample code in this skill included.
|
|
53
|
+
- Return **`null`** for a missing value, never `undefined` — or an empty array, or whatever the intended
|
|
54
|
+
default is
|
|
55
|
+
([SKILL.md](../SKILL.md#return-null-or-an-empty-array-for-a-missing-value-never-undefined)).
|
|
56
|
+
- Members are ordered: constructor → factory → static getter → static method → instance getter →
|
|
57
|
+
instance setter → instance method. Keep methods that call one another near each other.
|
|
58
|
+
- **Why fix the order**: when every class is ordered the same way, a reviewer's eye goes straight to
|
|
59
|
+
what is specific to this class. When the order varies per class, a deliberate difference and an
|
|
60
|
+
omission look alike.
|
|
61
|
+
|
|
62
|
+
## No additional HTTP client (restated)
|
|
63
|
+
|
|
64
|
+
**HTTP communication goes through Node.js's native `fetch` as a rule.** In the direct-HTTP pattern use
|
|
65
|
+
the `fetch` built into `BaseLauncher` and implement no HTTP client of your own. **Do not add another
|
|
66
|
+
HTTP client module such as axios.** Wrap an SDK only where the SDK owns the external communication
|
|
67
|
+
([sdk-wrapper.md](./sdk-wrapper.md) — an interim implementation). The reasoning is in
|
|
68
|
+
[SKILL.md](../SKILL.md#http-goes-through-the-native-fetch-only--no-axios-no-http-client-of-your-own).
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Launcher (base and derived, direct HTTP)
|
|
2
|
+
|
|
3
|
+
How to write a launcher that extends `BaseLauncher`. This file covers the **direct-HTTP pattern**,
|
|
4
|
+
where the built-in `fetch` handles the transport. To wrap an SDK instead, see
|
|
5
|
+
[sdk-wrapper.md](./sdk-wrapper.md). Referenced from [SKILL.md](../SKILL.md); the whole picture is in
|
|
6
|
+
[architecture.md](./architecture.md).
|
|
7
|
+
|
|
8
|
+
## Do not implement an HTTP client of your own
|
|
9
|
+
|
|
10
|
+
A launcher **only declares the connection settings (`clientConfig`) and the `Payload` / `Capsule` it
|
|
11
|
+
uses**. The transport is the Node.js native `fetch` built into `BaseLauncher`, so do not import axios
|
|
12
|
+
and do not write an HTTP client or Fetcher class of your own
|
|
13
|
+
([SKILL.md](../SKILL.md#http-goes-through-the-native-fetch-only--no-axios-no-http-client-of-your-own)).
|
|
14
|
+
|
|
15
|
+
- **Why**: the design collects the transport, retries, error conversion and stub-swapping for tests
|
|
16
|
+
into the single place that is `BaseLauncher`. A launcher that brings its own transport breaks that,
|
|
17
|
+
and the transport ends up implemented differently per operation.
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
// Bad: bringing in an HTTP client of your own, or axios
|
|
21
|
+
import axios from 'axios'
|
|
22
|
+
|
|
23
|
+
export default class CreateDocumentLauncher extends BaseDocumentApiLauncher {
|
|
24
|
+
async launchRequest ({ payload }) {
|
|
25
|
+
const response = await axios.post(/* ... */) // throws away BaseLauncher's fetch flow
|
|
26
|
+
// ...
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Base launcher: one per service, holding `clientConfig`
|
|
32
|
+
|
|
33
|
+
For each service (each external API) create one abstract base, `Base<Service>Launcher`, holding
|
|
34
|
+
`clientConfig` (the connection settings). Every operation's derived launcher extends it.
|
|
35
|
+
|
|
36
|
+
- `static get clientConfig ()` (override, **required**): returns `{ BASE_URL: ... }`. It becomes the
|
|
37
|
+
default `config` for `create()` and is used as `baseUrl` (= `config[baseUrlKey]`, the default key
|
|
38
|
+
being `'BASE_URL'`).
|
|
39
|
+
- Take the base URL, credentials and the like **from environment variables (`env`)**. Never hard-code
|
|
40
|
+
the values.
|
|
41
|
+
- When the response is not JSON, override `static get ResponseBodyParser ()` with
|
|
42
|
+
`BlobResponseBodyParser` or another parser (the default is `JsonResponseBodyParser`).
|
|
43
|
+
- `static get fetch ()` returns the native `fetch` by default. **This is what tests replace**
|
|
44
|
+
([testing.md](./testing.md)).
|
|
45
|
+
- **Why environment variables**: a base URL or an API token differs per environment (development /
|
|
46
|
+
staging / live) and is also a secret. Going through `env` absorbs the difference between
|
|
47
|
+
environments and keeps the secret out of the repository.
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
import {
|
|
51
|
+
env,
|
|
52
|
+
} from '../globals/_.js'
|
|
53
|
+
|
|
54
|
+
import {
|
|
55
|
+
BaseLauncher,
|
|
56
|
+
} from '@openreachtech/mentsu-rocket-client'
|
|
57
|
+
|
|
58
|
+
export default class BaseDocumentApiLauncher extends BaseLauncher {
|
|
59
|
+
/**
|
|
60
|
+
* get: client configuration.
|
|
61
|
+
*
|
|
62
|
+
* @override
|
|
63
|
+
* @returns {{
|
|
64
|
+
* BASE_URL: string
|
|
65
|
+
* }}
|
|
66
|
+
*/
|
|
67
|
+
static get clientConfig () {
|
|
68
|
+
return {
|
|
69
|
+
BASE_URL: env.DOCUMENT_API_BASE_URL,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Derived launcher: one per API operation, tying `Payload` / `Capsule` together
|
|
76
|
+
|
|
77
|
+
It extends the base launcher and declares the `Payload` / `Capsule` this operation uses. It holds no
|
|
78
|
+
logic — it returns two getters and nothing else.
|
|
79
|
+
|
|
80
|
+
- `static get Payload ()` (override, **required**): returns this operation's `Payload` class.
|
|
81
|
+
- `static get Capsule ()` (override, **required**): returns this operation's `Capsule` class.
|
|
82
|
+
- **Why only getters**: the send flow (validate → `fetch` → parse → build the `Capsule`) lives
|
|
83
|
+
entirely in `BaseLauncher`. The derived class works by declaring which input definition and which
|
|
84
|
+
output wrapper to use. Adding logic here breaks the first principle — differences stay confined to
|
|
85
|
+
Payload/Capsule.
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
import BaseDocumentApiLauncher from './BaseDocumentApiLauncher.js'
|
|
89
|
+
|
|
90
|
+
import CreateDocumentPayload from './CreateDocumentPayload.js'
|
|
91
|
+
import CreateDocumentCapsule from './CreateDocumentCapsule.js'
|
|
92
|
+
|
|
93
|
+
export default class CreateDocumentLauncher extends BaseDocumentApiLauncher {
|
|
94
|
+
/**
|
|
95
|
+
* get: Payload class.
|
|
96
|
+
*
|
|
97
|
+
* @override
|
|
98
|
+
* @returns {typeof CreateDocumentPayload}
|
|
99
|
+
*/
|
|
100
|
+
static get Payload () {
|
|
101
|
+
return CreateDocumentPayload
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* get: Capsule class.
|
|
106
|
+
*
|
|
107
|
+
* @override
|
|
108
|
+
* @returns {typeof CreateDocumentCapsule}
|
|
109
|
+
*/
|
|
110
|
+
static get Capsule () {
|
|
111
|
+
return CreateDocumentCapsule
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## `create()` adopts `clientConfig` when the argument is omitted
|
|
117
|
+
|
|
118
|
+
`BaseLauncher.create()` adopts `this.clientConfig` as `config` when called without arguments. The
|
|
119
|
+
caller gets a launcher carrying the connection settings assembled from environment variables with
|
|
120
|
+
nothing but `CreateDocumentLauncher.create()`.
|
|
121
|
+
|
|
122
|
+
- **Why**: so the caller never has to assemble the connection settings. The caller only cares which
|
|
123
|
+
service and which operation — that is, which class — and never writes the base URL handover again.
|
|
124
|
+
The steps for swapping it out through DI are in
|
|
125
|
+
[usage.md](./usage.md#di-inject-a-stub-launcher).
|
|
126
|
+
|
|
127
|
+
## The main members `BaseLauncher` provides
|
|
128
|
+
|
|
129
|
+
| Member | Kind | Role |
|
|
130
|
+
| :-- | :-- | :-- |
|
|
131
|
+
| `static create({ config })` | factory | Builds a launcher (`config` defaults to `clientConfig`) |
|
|
132
|
+
| `static createPayload({ pathParameterHash, query, body, optionHash })` | static | Shortcut for `this.Payload.create(...)` |
|
|
133
|
+
| `launchRequest({ payload, hooks })` | instance | The entry point from sending the request through to building the `Capsule` |
|
|
134
|
+
| `get baseUrl` | getter | `config[baseUrlKey]` |
|
|
135
|
+
|
|
136
|
+
- The argument of `launchRequest()` is an **object** (`{ payload }`). Passing it positionally
|
|
137
|
+
(`launchRequest(payload)`) leaves `payload` as `undefined` and the pre-send validation rejects the
|
|
138
|
+
request, so watch for that.
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Payload (the input definition, where differences are confined)
|
|
2
|
+
|
|
3
|
+
How to write a payload that extends `BasePayload`. **Every interface difference from the external API
|
|
4
|
+
— method, path, query and body key names, value formats, authentication — is confined here**
|
|
5
|
+
([SKILL.md](../SKILL.md#first-principle-confine-every-difference-from-the-external-api-to-payload--capsule-anti-corruption-layer)).
|
|
6
|
+
Authentication is in [authorization.md](./authorization.md); the output side is in
|
|
7
|
+
[capsule.md](./capsule.md).
|
|
8
|
+
|
|
9
|
+
## Required overrides: `method` / `pathname` (plus `contentType` when there is a body)
|
|
10
|
+
|
|
11
|
+
- `static get method ()`: the HTTP method. Return **a value of `REST_METHOD`** such as
|
|
12
|
+
`REST_METHOD.GET` — never a hard-coded string.
|
|
13
|
+
- `static get pathname ()`: the endpoint path. Write **the variable parts as `[placeholder]`**
|
|
14
|
+
(e.g. `/v1/projects/[projectId]/documents`). The name inside `[]` corresponds to a key of the
|
|
15
|
+
`pathParameterHash` passed to `create()`.
|
|
16
|
+
- `static get contentType ()`: needed only for methods that carry a body (`POST` / `PUT` / `PATCH`),
|
|
17
|
+
e.g. `'application/json'`. It is not called for methods without a body, so it can stay undefined.
|
|
18
|
+
- **Why `[placeholder]`**: declaring the variable parts lets `RequestPathParameterHash` interpolate
|
|
19
|
+
the values, and a missing key makes `hasInvalidParameterHash()` return `true` so the request becomes
|
|
20
|
+
**an error `Capsule` before it is sent**. Assembling the path by string concatenation defeats that
|
|
21
|
+
validation and an invalid URL goes out as-is.
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
// Good: variable parts as [placeholder], method from REST_METHOD
|
|
25
|
+
/** @override */
|
|
26
|
+
static get method () {
|
|
27
|
+
return REST_METHOD.GET
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @override */
|
|
31
|
+
static get pathname () {
|
|
32
|
+
return '/v1/projects/[projectId]/documents'
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
// Bad: assembling the path by string concatenation (no missing-key validation, differences not confined)
|
|
38
|
+
static get pathname () {
|
|
39
|
+
return `/v1/projects/${this.projectId}/documents` // NG
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Declare in `querySchema` / `bodySchema` only the keys that need conversion or validation
|
|
44
|
+
|
|
45
|
+
Declare the query and body schemas with the scalars from `@openreachtech/mentsu-schema` (`TextScalar` /
|
|
46
|
+
`KeywordScalar` / `IntegerScalar` / `DatetimeScalar`, …).
|
|
47
|
+
|
|
48
|
+
- A declared key becomes subject to **value normalization and validation** — `DatetimeScalar`, for
|
|
49
|
+
instance, converts to an ISO string on the way out.
|
|
50
|
+
- The default is `{}`. **Keys absent from the schema are still sent**, so declare only the keys that
|
|
51
|
+
need conversion or validation; the minimum is a fine place to start.
|
|
52
|
+
- **Why the minimum is enough**: the point of the schema is to shape values to the external API's spec
|
|
53
|
+
before sending. Declaring keys that need no conversion (a plain string, say) achieves nothing, and a
|
|
54
|
+
declaration that drifts from the actual spec is easy to misread. Declare what needs converting.
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
import {
|
|
58
|
+
KeywordScalar,
|
|
59
|
+
IntegerScalar,
|
|
60
|
+
} from '@openreachtech/mentsu-schema'
|
|
61
|
+
|
|
62
|
+
export default class FindDocumentsPayload extends BasePayload {
|
|
63
|
+
/** @type {Record<string, *>} */
|
|
64
|
+
static querySchema = {
|
|
65
|
+
keyword: KeywordScalar,
|
|
66
|
+
limit: IntegerScalar,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// method / pathname / authentication ...
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## When the outgoing keys are snake_case, swap in the Snake conversion classes
|
|
74
|
+
|
|
75
|
+
Write camelCase internally, and when the external API demands snake_case, absorb the difference by
|
|
76
|
+
returning a key-conversion class.
|
|
77
|
+
|
|
78
|
+
- `static get RequestQueryCtor ()`: returning `SnakeCasedKeyRequestQuery` makes the query keys
|
|
79
|
+
snake_case.
|
|
80
|
+
- `static get RequestBodyCtor ()`: returning `SnakeCasedKeyRequestBody` makes the body keys snake_case.
|
|
81
|
+
- **Why convert here**: confining the difference between our naming convention (camelCase) and the
|
|
82
|
+
external API's (snake_case) to the payload is the whole point of this module. The caller always
|
|
83
|
+
writes camelCase (`bodyText`) and it becomes `body_text` on the way out. If the caller has to think
|
|
84
|
+
about snake_case, the difference has leaked.
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import {
|
|
88
|
+
TextScalar,
|
|
89
|
+
} from '@openreachtech/mentsu-schema'
|
|
90
|
+
|
|
91
|
+
import {
|
|
92
|
+
BasePayload,
|
|
93
|
+
SnakeCasedKeyRequestBody,
|
|
94
|
+
REST_METHOD,
|
|
95
|
+
} from '@openreachtech/mentsu-rocket-client'
|
|
96
|
+
|
|
97
|
+
export default class CreateDocumentPayload extends BasePayload {
|
|
98
|
+
/** @type {Record<string, *>} */
|
|
99
|
+
static bodySchema = {
|
|
100
|
+
title: TextScalar,
|
|
101
|
+
bodyText: TextScalar, // SnakeCasedKeyRequestBody converts this to body_text on the way out
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @override */
|
|
105
|
+
static get method () {
|
|
106
|
+
return REST_METHOD.POST
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @override */
|
|
110
|
+
static get contentType () {
|
|
111
|
+
return 'application/json'
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @override */
|
|
115
|
+
static get pathname () {
|
|
116
|
+
return '/v1/projects/[projectId]/documents'
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* get: RequestBody constructor (camelCase -> snake_case).
|
|
121
|
+
*
|
|
122
|
+
* @override
|
|
123
|
+
* @returns {typeof SnakeCasedKeyRequestBody}
|
|
124
|
+
*/
|
|
125
|
+
static get RequestBodyCtor () {
|
|
126
|
+
return SnakeCasedKeyRequestBody
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// For authentication (AuthorizationBuilderCtor / authorizationApiKey) see authorization.md
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Inject fixed and default values with the `enrichXxx()` hooks
|
|
134
|
+
|
|
135
|
+
An API version that is always attached, a default `limit` and the like are injected with
|
|
136
|
+
`static enrichPathParameterHash ()` / `static enrichBody ()` / `static enrichQuery ()`.
|
|
137
|
+
|
|
138
|
+
- **Why**: so the caller is not forced to pass the same value every time. Injecting a fixed value is
|
|
139
|
+
the payload's responsibility too, and stays confined there.
|
|
140
|
+
|
|
141
|
+
## Define the input types with `@typedef`
|
|
142
|
+
|
|
143
|
+
Declare the shape of `pathParameterHash` / `query` / `body` with `@typedef`, so the caller can assemble
|
|
144
|
+
them with the right keys.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
/**
|
|
148
|
+
* @typedef {{
|
|
149
|
+
* projectId: string
|
|
150
|
+
* }} FindDocumentsPathParameterHash
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @typedef {{
|
|
155
|
+
* keyword?: string
|
|
156
|
+
* limit?: number
|
|
157
|
+
* }} FindDocumentsQuery
|
|
158
|
+
*/
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Build one with `Payload.create()`, or with `createPayload()` through the launcher
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
const payload = CreateDocumentPayload.create({
|
|
165
|
+
pathParameterHash: {
|
|
166
|
+
projectId: 'fake-project-id-001',
|
|
167
|
+
},
|
|
168
|
+
body: {
|
|
169
|
+
title: 'fake-title-001',
|
|
170
|
+
bodyText: 'fake-body-001',
|
|
171
|
+
},
|
|
172
|
+
})
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
- When you already hold the launcher, use `Launcher.createPayload({ ... })` — the shortcut for
|
|
176
|
+
`this.Payload.create(...)` ([usage.md](./usage.md)).
|