@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,192 @@
|
|
|
1
|
+
# Engine, context, and share (the app-wide layer)
|
|
2
|
+
|
|
3
|
+
Three classes set up the whole REST surface. They mirror the GraphQL engine/context/share: the
|
|
4
|
+
**engine** wires routes, middleware, auth, and the standard errors; the **context** is built fresh
|
|
5
|
+
per request; the **share** is a single per-process object.
|
|
6
|
+
|
|
7
|
+
## Engine
|
|
8
|
+
|
|
9
|
+
`AppRestfulApiServerEngine extends BaseRestfulApiServerEngine`. It is the single place that
|
|
10
|
+
configures the REST layer.
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import express from 'express'
|
|
14
|
+
import cors from 'cors'
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
BaseRestfulApiServerEngine,
|
|
18
|
+
} from '@openreachtech/renchan'
|
|
19
|
+
|
|
20
|
+
import rootPath from '../../app/globals/root-path.js'
|
|
21
|
+
|
|
22
|
+
import AppRestfulApiShare from './contexts/AppRestfulApiShare.js'
|
|
23
|
+
import AppRestfulApiContext from './contexts/AppRestfulApiContext.js'
|
|
24
|
+
|
|
25
|
+
export default class AppRestfulApiServerEngine extends BaseRestfulApiServerEngine {
|
|
26
|
+
/** @override */
|
|
27
|
+
static get config () {
|
|
28
|
+
return {
|
|
29
|
+
pathPrefix: '/v1', // prepended to every routePath; null = none
|
|
30
|
+
renderersPath: rootPath.to('server/restfulapi/renderers/v1/'),
|
|
31
|
+
staticPath: rootPath.to('public/'),
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @override */
|
|
36
|
+
static get Share () {
|
|
37
|
+
return AppRestfulApiShare
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @override */
|
|
41
|
+
static get Context () {
|
|
42
|
+
return AppRestfulApiContext
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ... standardErrorEnvelopHash, generateFilterHandler, visaIssuers, collectMiddleware ...
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- **`static get config()`** — `pathPrefix` (route prefix, e.g. `/v1`), `renderersPath` (the **one**
|
|
50
|
+
version directory whose renderers this engine serves), `staticPath` (static files root). To serve
|
|
51
|
+
a second version, run an engine pointed at `renderers/v2/` with `pathPrefix: '/v2'`.
|
|
52
|
+
- **`static get Share()` / `static get Context()`** — the app Share / Context classes (below).
|
|
53
|
+
- **`static get standardErrorEnvelopHash()`** — the **cross-cutting** error envelopes, keyed by
|
|
54
|
+
name, each `{ statusCode, errorMessage }`. The framework requires at least `Unknown`,
|
|
55
|
+
`ConcreteMemberNotFound`, `Unauthenticated`, `Unauthorized`, `Database`. These back the engine's
|
|
56
|
+
own `errorResponseHash` (used by the filter handler); per-endpoint errors live on the renderer
|
|
57
|
+
(`errorStructureHash`).
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
/** @override */
|
|
61
|
+
static get standardErrorEnvelopHash () {
|
|
62
|
+
return {
|
|
63
|
+
Unknown: {
|
|
64
|
+
statusCode: 500,
|
|
65
|
+
errorMessage: 'Unknown error',
|
|
66
|
+
},
|
|
67
|
+
Unauthenticated: {
|
|
68
|
+
statusCode: 401,
|
|
69
|
+
errorMessage: 'Unauthenticated',
|
|
70
|
+
},
|
|
71
|
+
Unauthorized: {
|
|
72
|
+
statusCode: 403,
|
|
73
|
+
errorMessage: 'Unauthorized',
|
|
74
|
+
},
|
|
75
|
+
// ConcreteMemberNotFound, Database, ...
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- **`collectMiddleware()`** — the express middleware stack (cors, JSON / urlencoded body parsers,
|
|
81
|
+
static file serving, …). Returns an array applied in order.
|
|
82
|
+
|
|
83
|
+
## The auth filter: `generateFilterHandler()` + `visaIssuers`
|
|
84
|
+
|
|
85
|
+
Authentication/authorization is a **cross-cutting filter** run before `render()` for every renderer
|
|
86
|
+
whose `passesFilter` is `false`. Two pieces:
|
|
87
|
+
|
|
88
|
+
- **`get visaIssuers()`** — async predicates that populate the request's **visa**, each given
|
|
89
|
+
`{ expressRequest, userEntity, engine }`:
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
/** @override */
|
|
93
|
+
get visaIssuers () {
|
|
94
|
+
return {
|
|
95
|
+
hasAuthenticated: async ({
|
|
96
|
+
userEntity,
|
|
97
|
+
}) => userEntity !== null,
|
|
98
|
+
hasAuthorized: async ({
|
|
99
|
+
userEntity,
|
|
100
|
+
}) => true,
|
|
101
|
+
hasPathPermission: async ({
|
|
102
|
+
userEntity,
|
|
103
|
+
}) => true,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
- **`generateFilterHandler()`** — returns an async `({ body, query, context, request }) => errorResponse | null`.
|
|
109
|
+
Read the visa via the context and **return an error response to reject**, or `null` to allow:
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
/** @override */
|
|
113
|
+
generateFilterHandler () {
|
|
114
|
+
return async ({
|
|
115
|
+
context,
|
|
116
|
+
}) => {
|
|
117
|
+
if (!context.hasAuthenticated()) {
|
|
118
|
+
return this.errorResponseHash.Unauthenticated.createAsError()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!context.hasAuthorized()) {
|
|
122
|
+
return this.errorResponseHash.Unauthorized.createAsError()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
A renderer with `get passesFilter () { return true }` skips this entirely (public endpoint).
|
|
131
|
+
|
|
132
|
+
## Context (per request)
|
|
133
|
+
|
|
134
|
+
`AppRestfulApiContext extends BaseRestfulApiContext`. The framework builds it **per request**:
|
|
135
|
+
it extracts the access token from the header (`x-renchan-access-token`), calls `findUser`, and
|
|
136
|
+
builds the visa from the engine's `visaIssuers`.
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
import {
|
|
140
|
+
BaseRestfulApiContext,
|
|
141
|
+
} from '@openreachtech/renchan'
|
|
142
|
+
|
|
143
|
+
export default class AppRestfulApiContext extends BaseRestfulApiContext {
|
|
144
|
+
/**
|
|
145
|
+
* Resolve the authenticated user from the access token.
|
|
146
|
+
*
|
|
147
|
+
* @override
|
|
148
|
+
* @param {{
|
|
149
|
+
* expressRequest: ExpressType.Request
|
|
150
|
+
* accessToken: string | null
|
|
151
|
+
* now?: Date
|
|
152
|
+
* }} params
|
|
153
|
+
* @returns {Promise<renchan.UserEntity | null>}
|
|
154
|
+
*/
|
|
155
|
+
static async findUser ({
|
|
156
|
+
expressRequest,
|
|
157
|
+
accessToken,
|
|
158
|
+
now = new Date(),
|
|
159
|
+
}) {
|
|
160
|
+
// look up the token → return the user entity, or null
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The context is what `render()` receives as `context`. It exposes:
|
|
166
|
+
|
|
167
|
+
- **`now`** — the request timestamp (`requestedAt`); use it so all writes in the request share one
|
|
168
|
+
time.
|
|
169
|
+
- **`userId` / `userEntity`** — the authenticated user (or `null`).
|
|
170
|
+
- **`share`** — the per-process share (below); **`env`** / **`NODE_ENV`** — configuration.
|
|
171
|
+
- **`hasAuthenticated()` / `hasAuthorized()` / `hasPathPermission()` / `canRender()`** — the visa
|
|
172
|
+
predicates the filter handler reads.
|
|
173
|
+
- **`uuid`** — a per-request id.
|
|
174
|
+
|
|
175
|
+
Override `findUser` to do the token → user lookup. Add domain-neutral aliases if useful (e.g. a
|
|
176
|
+
`provider` getter aliasing `userEntity`), but keep app-specific naming out of the base pattern.
|
|
177
|
+
|
|
178
|
+
## Share (per process)
|
|
179
|
+
|
|
180
|
+
`AppRestfulApiShare extends BaseRestfulApiShare`. A single object created once at startup, reachable
|
|
181
|
+
as `context.share`. It holds `env` and is the place for **shared, long-lived collaborators** (an
|
|
182
|
+
external-API client, a provider). Often a `// noop` subclass until something needs sharing:
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
import {
|
|
186
|
+
BaseRestfulApiShare,
|
|
187
|
+
} from '@openreachtech/renchan'
|
|
188
|
+
|
|
189
|
+
export default class AppRestfulApiShare extends BaseRestfulApiShare {
|
|
190
|
+
// noop
|
|
191
|
+
}
|
|
192
|
+
```
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# The renderer file
|
|
2
|
+
|
|
3
|
+
A renderer resolves one HTTP endpoint. It extends a **method-verb base** and implements a fixed
|
|
4
|
+
surface: the route, the errors it can return, whether it skips the auth filter, and `render()`.
|
|
5
|
+
|
|
6
|
+
> Examples use a generic `Article` resource. Swap in your own.
|
|
7
|
+
|
|
8
|
+
## Method-verb subclasses
|
|
9
|
+
|
|
10
|
+
Extend the base for the HTTP method the endpoint answers. The base fixes `static get method()`:
|
|
11
|
+
|
|
12
|
+
| Base class | method | Notes |
|
|
13
|
+
| --- | --- | --- |
|
|
14
|
+
| `BaseGetRenderer` | `get` | no request body |
|
|
15
|
+
| `BasePostRenderer` | `post` | body-bearing (multer middleware, file uploads) |
|
|
16
|
+
| `BasePutRenderer` | `put` | body-bearing |
|
|
17
|
+
| `BasePatchRenderer` | `patch` | body-bearing |
|
|
18
|
+
| `BaseDeleteRenderer` | `delete` | |
|
|
19
|
+
| `BaseHeadRenderer` / `BaseOptionsRenderer` / … | head / options / … | |
|
|
20
|
+
|
|
21
|
+
Body-bearing verbs (post/put/patch) extend a request-body base that wires **multer** for
|
|
22
|
+
`multipart/form-data`; the rest extend the plain renderer base directly. All ultimately extend
|
|
23
|
+
`BaseRenderer`, which holds the factory, the error hash, and `flushResponse()`.
|
|
24
|
+
|
|
25
|
+
## Directory & versioning
|
|
26
|
+
|
|
27
|
+
One renderer = one file (one class per file):
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
server/restfulapi/renderers/<version>/<method>/<Name>Renderer.js
|
|
31
|
+
e.g. server/restfulapi/renderers/v1/get/ArticleGetRenderer.js
|
|
32
|
+
server/restfulapi/renderers/v1/post/CreateArticleRenderer.js
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- The **folder encodes version and method** — the framework reads each renderer's `routePath` and
|
|
36
|
+
`method` and registers the express route under `config.pathPrefix`. You never wire routes by hand.
|
|
37
|
+
- **Class name** = `<Name>Renderer` (PascalCase). Let the `get/` `post/` folder carry the verb rather
|
|
38
|
+
than repeating it in the name.
|
|
39
|
+
- A new API **version** is a new directory (`renderers/v2/`) served by an engine pointed at it with
|
|
40
|
+
its own `pathPrefix` (`/v2`). See [engine-and-context.md](./engine-and-context.md).
|
|
41
|
+
|
|
42
|
+
## The fixed surface
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import {
|
|
46
|
+
BasePostRenderer,
|
|
47
|
+
RestfulApiResponse,
|
|
48
|
+
} from '@openreachtech/renchan'
|
|
49
|
+
|
|
50
|
+
import Article from '../../../../../sequelize/models/Article.js'
|
|
51
|
+
|
|
52
|
+
export default class CreateArticleRenderer extends BasePostRenderer {
|
|
53
|
+
/**
|
|
54
|
+
* get: Route path.
|
|
55
|
+
*
|
|
56
|
+
* @override
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
static get routePath () {
|
|
60
|
+
return '/articles'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* get: Error structure hash.
|
|
65
|
+
*
|
|
66
|
+
* @override
|
|
67
|
+
* @returns {Record<string, RestfulApiType.ErrorResponseEnvelope>}
|
|
68
|
+
*/
|
|
69
|
+
static get errorStructureHash () {
|
|
70
|
+
return {
|
|
71
|
+
InvalidRequestBody: {
|
|
72
|
+
statusCode: 400,
|
|
73
|
+
errorMessage: 'Invalid request body',
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* get: Passes filter.
|
|
80
|
+
*
|
|
81
|
+
* @override
|
|
82
|
+
* @returns {boolean}
|
|
83
|
+
*/
|
|
84
|
+
get passesFilter () {
|
|
85
|
+
return false // run the engine auth filter before render
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Render.
|
|
90
|
+
*
|
|
91
|
+
* @override
|
|
92
|
+
* @param {RestfulApiType.RenderInput<*, *>} input
|
|
93
|
+
* @returns {Promise<RestfulApiType.RenderResponse>}
|
|
94
|
+
*/
|
|
95
|
+
async render ({
|
|
96
|
+
body,
|
|
97
|
+
context: {
|
|
98
|
+
now,
|
|
99
|
+
userId,
|
|
100
|
+
},
|
|
101
|
+
}) {
|
|
102
|
+
if (!this.isValidBody({
|
|
103
|
+
body,
|
|
104
|
+
})) {
|
|
105
|
+
return this.errorResponseHash.InvalidRequestBody.createAsError()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const article = await this.saveArticle({
|
|
109
|
+
body,
|
|
110
|
+
now,
|
|
111
|
+
userId,
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
return RestfulApiResponse.create({
|
|
115
|
+
statusCode: 201,
|
|
116
|
+
content: {
|
|
117
|
+
articleId: article.id,
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ... isValidBody(), saveArticle() ...
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
- **`static get routePath()`** — the route, prefixed by the engine (`/articles` → `/v1/articles`).
|
|
127
|
+
- **`static get errorStructureHash()`** — the errors this renderer can return, each with a
|
|
128
|
+
`statusCode` + `errorMessage`. See [response-and-flusher.md](./response-and-flusher.md).
|
|
129
|
+
- **`get passesFilter()`** — `true` = public (skip the engine auth filter); default `false` = run it.
|
|
130
|
+
- **`async render(input)`** — the entry point (below).
|
|
131
|
+
|
|
132
|
+
## `render()` input: `{ body, query, context, request }`
|
|
133
|
+
|
|
134
|
+
The framework passes one object with four keys:
|
|
135
|
+
|
|
136
|
+
- **`body`** — the parsed request body (POST/PUT/PATCH). For `multipart/form-data`, multer has
|
|
137
|
+
already run and files are attached (see file uploads below).
|
|
138
|
+
- **`query`** — `expressRequest.query` (the query-string params, all strings).
|
|
139
|
+
- **`context`** — the per-request context: `now`, `share`, `env`, `userId` / `userEntity`, and the
|
|
140
|
+
visa predicates. See [engine-and-context.md](./engine-and-context.md).
|
|
141
|
+
- **`request`** — the wrapped express request. `request.expressRequest` is the raw request;
|
|
142
|
+
`request.pathParameterHashProxy` reads path params (`/articles/:articleId`) and returns **`null`**
|
|
143
|
+
for a missing key rather than `undefined`.
|
|
144
|
+
|
|
145
|
+
Destructure only what the endpoint needs. `render()` must **return a `RestfulApiResponse`** on every
|
|
146
|
+
path — success via `RestfulApiResponse.create(...)`, failure via
|
|
147
|
+
`this.errorResponseHash.Xxx.createAsError()`. Do not throw (a thrown error becomes a generic 500).
|
|
148
|
+
|
|
149
|
+
## Dependency injection (optional)
|
|
150
|
+
|
|
151
|
+
Like a GraphQL resolver, a renderer needs no constructor unless it depends on a tool that tests must
|
|
152
|
+
substitute (a token/id generator, an external client). Then add the factory triple — and pass the
|
|
153
|
+
error hash through:
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
constructor ({
|
|
157
|
+
randomTextGenerator,
|
|
158
|
+
errorResponseHash,
|
|
159
|
+
}) {
|
|
160
|
+
super({
|
|
161
|
+
errorResponseHash,
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
this.randomTextGenerator = randomTextGenerator
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static create ({
|
|
168
|
+
randomTextGenerator = this.createRandomTextGenerator(),
|
|
169
|
+
errorStructureHash = this.errorStructureHash,
|
|
170
|
+
} = {}) {
|
|
171
|
+
const errorResponseHash = this.buildErrorResponseHash({
|
|
172
|
+
errorStructureHash,
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
return new this({
|
|
176
|
+
randomTextGenerator,
|
|
177
|
+
errorResponseHash,
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static createRandomTextGenerator () {
|
|
182
|
+
return RandomTextGenerator.create()
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
- Note the base's `create()` builds `errorResponseHash` from `errorStructureHash` via
|
|
187
|
+
`buildErrorResponseHash()`. When you override `create()`, do that build yourself and pass
|
|
188
|
+
`errorResponseHash` to `super()` — never drop it.
|
|
189
|
+
|
|
190
|
+
## File uploads (body-bearing renderers)
|
|
191
|
+
|
|
192
|
+
A body-bearing renderer (post/put/patch) declares the multipart file fields it accepts via
|
|
193
|
+
`static get fileFieldsConfigHash()` — `{ <fieldName>: <maxCount> }`. The base builds the multer
|
|
194
|
+
middleware from it; with an empty hash multer runs in `.none()` mode (parses fields, no files).
|
|
195
|
+
|
|
196
|
+
```js
|
|
197
|
+
static get fileFieldsConfigHash () {
|
|
198
|
+
return {
|
|
199
|
+
attachment: 1,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Uploaded files are attached to the request and merged into `body` before `render()` runs.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# Response, error hash, and flusher
|
|
2
|
+
|
|
3
|
+
A renderer always returns a **`RestfulApiResponse`** — an HTTP envelope carrying a `statusCode` plus
|
|
4
|
+
`content` (success) or `error` (failure). A **flusher** then writes that envelope to the express
|
|
5
|
+
response. This file covers both, and the error hash that produces error responses.
|
|
6
|
+
|
|
7
|
+
## RestfulApiResponse
|
|
8
|
+
|
|
9
|
+
`RestfulApiResponse` holds `{ statusCode, headers, content, error }`.
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
// success
|
|
13
|
+
return RestfulApiResponse.create({
|
|
14
|
+
statusCode: 200,
|
|
15
|
+
content: {
|
|
16
|
+
articleId: article.id,
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- **Success** → `RestfulApiResponse.create({ statusCode, headers?, content })`. `content` is the
|
|
22
|
+
response payload; `error` stays `null`.
|
|
23
|
+
- **Failure** → **do not** `new` an error response by hand; take it from the built error hash:
|
|
24
|
+
`this.errorResponseHash.Xxx.createAsError()` (below). `createAsError()` fills in the entry's
|
|
25
|
+
`statusCode` and wraps the message as `{ error: { message } }`.
|
|
26
|
+
- `get status()` aliases `statusCode` (the flusher reads `renderResponse.status`).
|
|
27
|
+
|
|
28
|
+
## Error hash: `errorStructureHash` → `errorResponseHash`
|
|
29
|
+
|
|
30
|
+
Like the GraphQL error code hash, a renderer declares every error it can return **up front**. The
|
|
31
|
+
difference: **each REST entry carries a `statusCode` and `errorMessage`**, because an HTTP response
|
|
32
|
+
needs a status — a code alone is not enough.
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
/** @override */
|
|
36
|
+
static get errorStructureHash () {
|
|
37
|
+
return {
|
|
38
|
+
InvalidRequestBody: {
|
|
39
|
+
statusCode: 400,
|
|
40
|
+
errorMessage: 'Invalid request body',
|
|
41
|
+
},
|
|
42
|
+
ArticleNotFound: {
|
|
43
|
+
statusCode: 404,
|
|
44
|
+
errorMessage: 'Article not found',
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
- The framework's `buildErrorResponseHash()` turns each `{ Name: { statusCode, errorMessage } }`
|
|
51
|
+
entry into a constructable `RestfulApiResponse` subclass on **`this.errorResponseHash`** (also
|
|
52
|
+
reachable as `this.Error`). Return one with `this.errorResponseHash.ArticleNotFound.createAsError()`.
|
|
53
|
+
- **Naming = the reason** (`InvalidRequestBody`, `ArticleNotFound`) — read as a sentence at the
|
|
54
|
+
return site. No `info` / `data` suffixes.
|
|
55
|
+
- **Cross-cutting** errors (`Unauthenticated`, `Unauthorized`, `Unknown`, `Database`) are declared
|
|
56
|
+
**once on the engine** (`standardErrorEnvelopHash`) and used by the auth filter — do not redeclare
|
|
57
|
+
them per renderer. See [engine-and-context.md](./engine-and-context.md).
|
|
58
|
+
|
|
59
|
+
This is the REST analog of "declare errors as a hash, throw by name" — here you **return** by name,
|
|
60
|
+
and each name resolves to a status-bearing response.
|
|
61
|
+
|
|
62
|
+
## Extending RestfulApiResponse
|
|
63
|
+
|
|
64
|
+
When the default envelope is not enough — a custom body shape, extra headers, a computed status —
|
|
65
|
+
subclass `RestfulApiResponse` in the app and return that subclass. Override what you need (e.g. a
|
|
66
|
+
fixed `statusCode` getter, or an added factory) while keeping the `{ statusCode, content, error }`
|
|
67
|
+
contract the flusher relies on. Prefer extending over hand-building one-off response objects, so
|
|
68
|
+
every endpoint returns the same recognizable shape.
|
|
69
|
+
|
|
70
|
+
## Flushers
|
|
71
|
+
|
|
72
|
+
A **flusher** (`BaseRestfulApiResponseFlusher` subclass) writes the `RestfulApiResponse` to express:
|
|
73
|
+
status → headers → body. The renderer picks one via `static get FlusherCtor()`.
|
|
74
|
+
|
|
75
|
+
| Flusher | contentType | body |
|
|
76
|
+
| --- | --- | --- |
|
|
77
|
+
| `JsonRestfulApiResponseFlusher` (default) | `application/json` | `{ content, error }` |
|
|
78
|
+
| `HtmlRestfulApiResponseFlusher` | `text/html` | HTML string |
|
|
79
|
+
| `CsvRestfulApiResponseFlusher` | `text/csv` | CSV |
|
|
80
|
+
| app custom (e.g. a redirect flusher) | your choice | your choice |
|
|
81
|
+
|
|
82
|
+
- **Default is JSON** — a renderer that returns JSON needs no `FlusherCtor` override.
|
|
83
|
+
- **Select a non-default flusher** per renderer:
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
/** @override */
|
|
87
|
+
static get FlusherCtor () {
|
|
88
|
+
return HtmlRestfulApiResponseFlusher
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- **Write a custom flusher** by extending `BaseRestfulApiResponseFlusher`: set `static get
|
|
93
|
+
contentType()` and implement `flushResponseBody()` (or override `flushResponse()` entirely for
|
|
94
|
+
non-body responses like a redirect). It reads `this.renderResponse` (the `RestfulApiResponse`) and
|
|
95
|
+
`this.expressResponse`:
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
import {
|
|
99
|
+
BaseRestfulApiResponseFlusher,
|
|
100
|
+
} from '@openreachtech/renchan'
|
|
101
|
+
|
|
102
|
+
export default class RedirectRestfulApiResponseFlusher extends BaseRestfulApiResponseFlusher {
|
|
103
|
+
/** @override */
|
|
104
|
+
static get contentType () {
|
|
105
|
+
return 'text/html'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @override */
|
|
109
|
+
flushResponse () {
|
|
110
|
+
this.expressResponse.redirect(
|
|
111
|
+
this.renderResponse.status,
|
|
112
|
+
this.renderResponse.content?.redirectUrl ?? '/'
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Place app flushers under `server/restfulapi/flushers/`.
|