@softistx/riva-rest 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +151 -2
- package/dist/bff/index.d.ts +15 -0
- package/dist/bff/index.d.ts.map +1 -0
- package/dist/bff/index.js +21 -0
- package/dist/bff/index.js.map +10 -0
- package/dist/chunks/index-9628zgfc.js +8 -0
- package/dist/chunks/index-9628zgfc.js.map +10 -0
- package/dist/chunks/index-ytrzmcp0.js +28 -0
- package/dist/chunks/index-ytrzmcp0.js.map +10 -0
- package/dist/entities/index.d.ts +27 -0
- package/dist/entities/index.d.ts.map +1 -0
- package/dist/entities/index.js +38 -0
- package/dist/entities/index.js.map +10 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/http-transport.d.ts +13 -0
- package/dist/http-transport.d.ts.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +148 -0
- package/dist/index.js.map +14 -0
- package/dist/operation.d.ts +16 -0
- package/dist/operation.d.ts.map +1 -0
- package/dist/query.d.ts +44 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/server/index.d.ts +18 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +45 -0
- package/dist/server/index.js.map +10 -0
- package/dist/transport.d.ts +38 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/types.d.ts +42 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/wire.d.ts +5 -0
- package/dist/wire.d.ts.map +1 -0
- package/docs/README.md +19 -0
- package/docs/guide/bff.md +92 -0
- package/docs/guide/cache.md +44 -0
- package/docs/guide/errors.md +33 -0
- package/docs/guide/free-fetcher.md +47 -0
- package/docs/guide/openapi.md +45 -0
- package/docs/guide/transport.md +49 -0
- package/docs/troubleshooting.md +184 -0
- package/package.json +85 -5
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Transport
|
|
2
|
+
|
|
3
|
+
## What it is for
|
|
4
|
+
|
|
5
|
+
Definitions are stateless; the transport lives on the `RivaClient` that runs them. Give each client one when you create it (on the server, one per request).
|
|
6
|
+
|
|
7
|
+
## OpenAPI mode wiring
|
|
8
|
+
|
|
9
|
+
The OpenAPI mode reads the operations the spec describes, which `@softistx/openapi-codegen` generates:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bunx nxgt-openapi generate -i openapi.yaml # writes generated/openapi
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// api.ts
|
|
17
|
+
import { createHttpClient } from '@softistx/httpyz';
|
|
18
|
+
import { createOpenApiClient } from '@softistx/openapi-httpyz';
|
|
19
|
+
import { createRivaClient } from '@softistx/riva';
|
|
20
|
+
import { httpyzTransport, provideRest } from '@softistx/riva-rest';
|
|
21
|
+
import { operations } from './generated/openapi/operations'; // from `nxgt-openapi generate`
|
|
22
|
+
|
|
23
|
+
const http = createHttpClient({ baseUrl: 'https://api.example.com', timeout: 10_000 });
|
|
24
|
+
export const api = createOpenApiClient(http, operations);
|
|
25
|
+
|
|
26
|
+
export const client = createRivaClient();
|
|
27
|
+
provideRest(client, httpyzTransport(http, api));
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## `httpyzTransport(http, api?)`
|
|
31
|
+
|
|
32
|
+
| Argument | Meaning |
|
|
33
|
+
|---|---|
|
|
34
|
+
| `http` | The httpyz client. Its middleware, auth, retries and timeouts apply to every call. |
|
|
35
|
+
| `api` | The OpenAPI client (`createOpenApiClient`). Pass it for the OpenAPI mode; without it only [free fetchers](./free-fetcher.md) run. |
|
|
36
|
+
|
|
37
|
+
## Free-fetcher-only wiring
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
provideRest(client, httpyzTransport(http)); // no spec: free fetchers only
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Traps
|
|
44
|
+
|
|
45
|
+
- A read on a client with no transport throws `RivaScopeError` (the SSR leak guard): there is no module-level client. Provide it where you create the client.
|
|
46
|
+
- A transport with no OpenAPI client cannot run an OpenAPI definition: it fails with `RIVA_REST_UNWIRED`.
|
|
47
|
+
- On the server, create the `http` client inside the request, so its headers (a cookie) belong to that request.
|
|
48
|
+
|
|
49
|
+
Fixes: [troubleshooting](../troubleshooting.md).
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
Each entry is headed by the message you will see.
|
|
4
|
+
|
|
5
|
+
## `No REST transport on this RivaClient: call provideRest(client, httpyzTransport(http, api)) when you create it.`
|
|
6
|
+
|
|
7
|
+
Code: `RIVA_SCOPE` (`RivaScopeError`).
|
|
8
|
+
|
|
9
|
+
**When:** a query or mutation ran on a `RivaClient` that was never given a transport.
|
|
10
|
+
|
|
11
|
+
**Why:** definitions are stateless and the transport lives on the client, so two server requests never share one.
|
|
12
|
+
|
|
13
|
+
**Fix:** provide it where the client is created, per request on the server.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createHttpClient } from '@softistx/httpyz';
|
|
17
|
+
import { createRivaClient } from '@softistx/riva';
|
|
18
|
+
import { httpyzTransport, provideRest } from '@softistx/riva-rest';
|
|
19
|
+
|
|
20
|
+
const http = createHttpClient({ baseUrl: 'https://api.example.com' });
|
|
21
|
+
const client = createRivaClient();
|
|
22
|
+
provideRest(client, httpyzTransport(http));
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## `The REST transport of this RivaClient holds no httpyz client: build it with httpyzTransport(http, api).`
|
|
26
|
+
|
|
27
|
+
Code: `RIVA_SCOPE` (`RivaScopeError`).
|
|
28
|
+
|
|
29
|
+
**When:** a free fetcher read `http` from its context, and the transport is a custom one with no `http`.
|
|
30
|
+
|
|
31
|
+
**Fix:** use `httpyzTransport(http, api?)`, or give your transport an `http` member.
|
|
32
|
+
|
|
33
|
+
## `This REST transport has no OpenAPI client: build it with httpyzTransport(http, api) to run OpenAPI definitions.`
|
|
34
|
+
|
|
35
|
+
Code: `RIVA_REST_UNWIRED` (`RivaRequestError`, never retried).
|
|
36
|
+
|
|
37
|
+
**When:** an OpenAPI definition (`query(api, 'get /x')`) ran through `httpyzTransport(http)` made without `api`.
|
|
38
|
+
|
|
39
|
+
**Fix:** pass the client `createOpenApiClient(http, operations)` made as the second argument.
|
|
40
|
+
|
|
41
|
+
## `riva-rest: the OpenAPI client has no GET /nope.`
|
|
42
|
+
|
|
43
|
+
Code: `RIVA_REST_UNWIRED` (`RivaRequestError`, never retried), thrown when the query runs.
|
|
44
|
+
|
|
45
|
+
**Why:** the transport's `api` is not the client the definition came from: it has no operation at that method and path (two specs, or a client made over another `operations` table).
|
|
46
|
+
|
|
47
|
+
**Fix:** build the transport over the same client the definitions use, with the same `decode` setting: the data types follow the definitions' client, while the call runs on the transport's.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
export const api = createOpenApiClient(http, operations);
|
|
51
|
+
provideRest(client, httpyzTransport(http, api)); // the api the definitions take
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## `riva-rest: the OpenAPI client has no operation at GET /nope; check the path as the spec writes it.`
|
|
55
|
+
|
|
56
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when the definition is created.
|
|
57
|
+
|
|
58
|
+
**Why:** the route is not in the client's operation table. The path is the template as the spec writes it (`/invoices/{id}`, not `/invoices/:id`), and the method is lowercase in the route (`'get /invoices/{id}'`).
|
|
59
|
+
|
|
60
|
+
**Fix:** copy the route from the spec. TypeScript suggests the routes the spec has.
|
|
61
|
+
|
|
62
|
+
## `riva-rest: "invoices" is not a route; write it as '<method> <path>' (get /invoices/{id}).`
|
|
63
|
+
|
|
64
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when the definition is created.
|
|
65
|
+
|
|
66
|
+
**Fix:** write the method and the path, separated by one space, the path starting with `/`.
|
|
67
|
+
|
|
68
|
+
## `riva-rest: GET /health only reads; define it with query(), not mutation().`
|
|
69
|
+
|
|
70
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when the definition is created.
|
|
71
|
+
|
|
72
|
+
**Fix:** a `GET`, `HEAD` or `OPTIONS` operation is a `query()`. The reverse, `riva-rest: DELETE /x changes data; define it with mutation(), not query().`, is the same rule for `PUT`, `PATCH` and `DELETE`. A `POST` that only reads (a search) is a query.
|
|
73
|
+
|
|
74
|
+
## `riva-rest: query() needs an id; a function has no name to derive one from.`
|
|
75
|
+
|
|
76
|
+
Code: `RIVA_DEFINITION` (`RivaError`) (and `riva-rest: mutation() needs an id; …`), thrown when the definition is created.
|
|
77
|
+
|
|
78
|
+
**Why:** the cache key is the id plus the variables; an OpenAPI route gives one, a free fetcher does not, and a counter would differ between the server and the browser.
|
|
79
|
+
|
|
80
|
+
**Fix:** give the free fetcher an `id`.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
query({ id: 'todos', fetch: async () => [] as string[] });
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## `HTTP_404` (`RivaHttpError`)
|
|
87
|
+
|
|
88
|
+
**When:** the server answered a non-2xx status: one the spec declares (the reply is the error's `cause`, with its `data`), or one it does not (`UndeclaredStatusError` in the cause).
|
|
89
|
+
|
|
90
|
+
**Why:** a query's data is the 2xx reply, so any other status is a failure. It is retried only for 5xx, 408 and 429.
|
|
91
|
+
|
|
92
|
+
**Fix:** read the body from `error.cause.data` for a declared reply, or declare the status in the spec.
|
|
93
|
+
|
|
94
|
+
## `A free fetcher uses httpyz, which is server-only: …`
|
|
95
|
+
|
|
96
|
+
Code: `RIVA_SCOPE` (`RivaScopeError`).
|
|
97
|
+
|
|
98
|
+
**When:** in the browser, with `provideRest(client, bffTransport())`, a free fetcher (`query({ id, fetch })`) read `http` from its context.
|
|
99
|
+
|
|
100
|
+
**Why:** the BFF transport posts to `/_data` and holds no httpyz client; a free fetcher is arbitrary code against the API, so it only runs on the server.
|
|
101
|
+
|
|
102
|
+
**Fix:** run it in a loader (on the server), or describe the call in the OpenAPI spec and define it with `query(api, 'get /path')`, which the browser can run through `/_data`.
|
|
103
|
+
|
|
104
|
+
## `RIVA_DATA_UNKNOWN` (404) on an operation that exists
|
|
105
|
+
|
|
106
|
+
Code: `RIVA_DATA_UNKNOWN`, message `Unknown operation.` (`RivaRequestError`, status 404).
|
|
107
|
+
|
|
108
|
+
**When:** a call through `bffTransport` is answered 404 although the route is in the spec.
|
|
109
|
+
|
|
110
|
+
**Why:** the id is `rest:<operationId>`, read from the spec's operation table. Either the server's `operations(api)` was built from another spec (a stale generated `operations.ts`, a different version), the route has no `operationId` in the spec (the table is keyed by it: add one), or `operations(api)` is not in `dataRoute({ sources })` at all.
|
|
111
|
+
|
|
112
|
+
**Fix:** build `operations()` from the same `createOpenApiClient` the browser's definitions use, give the operation an `operationId` and regenerate, and check `sources`.
|
|
113
|
+
|
|
114
|
+
## `The data route answered N with a body that is not JSON.`
|
|
115
|
+
|
|
116
|
+
Code: `RIVA_INVALID_RESPONSE` (`RivaRequestError`), not retried. The variant `The data route answered without a "data" field.` is a 2xx JSON body that is not the route's `{ data }`.
|
|
117
|
+
|
|
118
|
+
**When:** a call through `bffTransport` got HTML or text back (N is the status).
|
|
119
|
+
|
|
120
|
+
**Why:** the request did not reach `dataRoute`: a proxy or CDN page, a login redirect, the route not mounted (the app's own 404 page), or a wrong `url`.
|
|
121
|
+
|
|
122
|
+
**Fix:** check the `url` option (default `/_data`), mount `dataRoute` on a route that exports `action`, and look at the response in the browser's network tab.
|
|
123
|
+
|
|
124
|
+
## Which riva error an httpyz error becomes
|
|
125
|
+
|
|
126
|
+
See the [error table in the README](../README.md#errors). The entries below add the cause and the fix.
|
|
127
|
+
|
|
128
|
+
## `RIVA_VALIDATION` and `RIVA_INVALID_RESPONSE` (`RivaRequestError`)
|
|
129
|
+
|
|
130
|
+
**When:** `openapi-httpyz` validates by default. `RIVA_VALIDATION`: the input breaks the spec (`page: 0` where the minimum is 1), and nothing was sent. `RIVA_INVALID_RESPONSE`: the reply does not match the spec, or has a media type it does not declare.
|
|
131
|
+
|
|
132
|
+
**Why:** neither is retried, since the same request would fail again.
|
|
133
|
+
|
|
134
|
+
**Fix:** correct the input, or the spec if the server drifted. The `ValidationError` in `error.cause.failure.issues` lists each issue.
|
|
135
|
+
|
|
136
|
+
## `RIVA_NETWORK` (`RivaNetworkError`)
|
|
137
|
+
|
|
138
|
+
**When:** no connection, a refused one, a CORS refusal or a timeout. It is retried per the definition's `retry` ([defaults in the core README](https://github.com/softistx/riva/blob/develop/packages/riva/README.md): 3 in a browser, 0 on the server).
|
|
139
|
+
|
|
140
|
+
## Traps
|
|
141
|
+
|
|
142
|
+
Entries with no message of their own, or with one the core throws.
|
|
143
|
+
|
|
144
|
+
## `This client already uses other entity identity rules. Share one entities() (or the same options objects) between its queries.`
|
|
145
|
+
|
|
146
|
+
Code: `RIVA_CACHE`.
|
|
147
|
+
|
|
148
|
+
**When:** two `restEntities()` calls were used on one client, even with the same `types`: each call makes a policy of its own.
|
|
149
|
+
|
|
150
|
+
**Fix:** create the policy once and share that very object between the client's definitions.
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
export const cache = restEntities({ Invoice: { is: (o) => 'number' in o, key: (o) => o['id'] as string } });
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## An entity does not update the other queries
|
|
157
|
+
|
|
158
|
+
**Why:** only an object `is` recognises is stored as an entity, and it needs the `key` to return a string or number. An object that already has a `__typename` keeps it.
|
|
159
|
+
|
|
160
|
+
**Fix:** check `is` against a real response object, and that the queries share the same `restEntities()` policy.
|
|
161
|
+
|
|
162
|
+
## A recognised object is the wrong type
|
|
163
|
+
|
|
164
|
+
**Why:** the first type whose `is` matches wins, so a loose check (`'id' in o`) tags every resource as the first type.
|
|
165
|
+
|
|
166
|
+
**Fix:** make `is` discriminate on something only that resource has.
|
|
167
|
+
|
|
168
|
+
## A request refuses a field called `__typename`
|
|
169
|
+
|
|
170
|
+
**Why:** `restEntities` injects `__typename` into the data. Data sent back as a request body, with request validation on, fails on the field.
|
|
171
|
+
|
|
172
|
+
**Fix:** drop it before sending: `const { __typename, ...body } = invoice`.
|
|
173
|
+
|
|
174
|
+
## REST and GraphQL entities on one client
|
|
175
|
+
|
|
176
|
+
**Why:** a client holds one set of identity rules, so a REST policy and a GraphQL one cannot share its store in v1.
|
|
177
|
+
|
|
178
|
+
**Fix:** use the entity policy for one of them, and request-keyed caching (the default) for the other.
|
|
179
|
+
|
|
180
|
+
## `riva-rest: DELETE /x changes data; define it with mutation(), not query().`
|
|
181
|
+
|
|
182
|
+
Code: `RIVA_DEFINITION` (`RivaError`), thrown when the definition is created, for a `PUT`, `PATCH` or `DELETE` route given to `query()`.
|
|
183
|
+
|
|
184
|
+
**Fix:** use `mutation(api, 'delete /x')`. Only `GET`, `HEAD`, `OPTIONS` and a `POST` that only reads (a search) are queries.
|
package/package.json
CHANGED
|
@@ -1,6 +1,86 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@softistx/riva-rest",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "REST transport for riva, typed by OpenAPI through openapi-httpyz, with a free fetcher for schema-less APIs.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"docs",
|
|
13
|
+
"README.md",
|
|
14
|
+
"package.json",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./entities": {
|
|
24
|
+
"types": "./dist/entities/index.d.ts",
|
|
25
|
+
"import": "./dist/entities/index.js",
|
|
26
|
+
"default": "./dist/entities/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./server": {
|
|
29
|
+
"types": "./dist/server/index.d.ts",
|
|
30
|
+
"import": "./dist/server/index.js",
|
|
31
|
+
"default": "./dist/server/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./bff": {
|
|
34
|
+
"types": "./dist/bff/index.d.ts",
|
|
35
|
+
"import": "./dist/bff/index.js",
|
|
36
|
+
"default": "./dist/bff/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"state",
|
|
42
|
+
"signals",
|
|
43
|
+
"cache",
|
|
44
|
+
"graphql",
|
|
45
|
+
"rest",
|
|
46
|
+
"ssr"
|
|
47
|
+
],
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/softistx/riva.git",
|
|
51
|
+
"directory": "packages/riva-rest"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"registry": "https://registry.npmjs.org/",
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "bun run ../../build.ts",
|
|
59
|
+
"test": "bun test src",
|
|
60
|
+
"typecheck": "bun check"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@softistx/httpyz": "^0.6.1",
|
|
64
|
+
"@softistx/openapi-httpyz": "^0.4.1",
|
|
65
|
+
"@softistx/riva": "0.1.0",
|
|
66
|
+
"@types/bun": "^1.4.5"
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"@softistx/riva": "^0.1.0",
|
|
70
|
+
"@softistx/httpyz": "^0.6.1",
|
|
71
|
+
"@softistx/openapi-httpyz": "^0.4.1"
|
|
72
|
+
},
|
|
73
|
+
"peerDependenciesMeta": {
|
|
74
|
+
"@softistx/openapi-httpyz": {
|
|
75
|
+
"optional": true
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"nxgt": {
|
|
79
|
+
"entrypoints": [
|
|
80
|
+
"src/index.ts",
|
|
81
|
+
"src/entities/index.ts",
|
|
82
|
+
"src/server/index.ts",
|
|
83
|
+
"src/bff/index.ts"
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
}
|