@owlmeans/api 0.1.2 → 0.1.3
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/README.md +26 -256
- package/build/service.js +1 -1
- package/build/service.js.map +1 -1
- package/build/types.d.ts +2 -2
- package/build/types.d.ts.map +1 -1
- package/build/utils/handler.d.ts +1 -1
- package/build/utils/handler.d.ts.map +1 -1
- package/build/utils/handler.js +5 -5
- package/build/utils/handler.js.map +1 -1
- package/package.json +12 -10
- package/src/service.ts +2 -2
- package/src/types.ts +2 -2
- package/src/utils/handler.ts +7 -7
- package/tsconfig.json +5 -9
package/README.md
CHANGED
|
@@ -1,288 +1,58 @@
|
|
|
1
1
|
# @owlmeans/api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
HTTP API client service for OwlMeans client contexts — handles module-based requests with auth token injection.
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
- **Error Handling**: Comprehensive error handling with resilient error types
|
|
12
|
-
- **URL Management**: Automatic URL construction from route definitions
|
|
13
|
-
- **Request/Response Processing**: Advanced request transformation and response processing
|
|
14
|
-
- **Security Features**: Integrated security helpers and header management
|
|
7
|
+
- `createApiService(alias?)` — creates an HTTP API client service
|
|
8
|
+
- `appendApiClient(ctx, alias?)` — registers the API client in the context
|
|
9
|
+
- `ApiClient` — the service interface; handles `GET`/`POST`/`PUT`/`DELETE` requests
|
|
10
|
+
- Error classes: `ApiError`, `ApiClientError`, `ServerCrashedError`, `ServerAuthError`
|
|
15
11
|
|
|
16
12
|
## Installation
|
|
17
13
|
|
|
18
14
|
```bash
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Core Concepts
|
|
23
|
-
|
|
24
|
-
### API Client Service
|
|
25
|
-
|
|
26
|
-
The API client is implemented as an OwlMeans service that can be registered with the context system. It provides a `handler` function that processes module requests and returns appropriate responses.
|
|
27
|
-
|
|
28
|
-
### Module-Based Requests
|
|
29
|
-
|
|
30
|
-
Instead of making direct HTTP calls, the API client works with OwlMeans Common Modules, which define:
|
|
31
|
-
- Route structure and methods
|
|
32
|
-
- Request/response types
|
|
33
|
-
- Parameter validation
|
|
34
|
-
- URL patterns
|
|
35
|
-
|
|
36
|
-
### Automatic Authentication
|
|
37
|
-
|
|
38
|
-
The client automatically handles authentication tokens:
|
|
39
|
-
- Retrieves tokens from the authentication service
|
|
40
|
-
- Includes tokens in request headers
|
|
41
|
-
- Processes token updates from server responses
|
|
42
|
-
- Integrates with the OwlMeans authentication subsystem
|
|
43
|
-
|
|
44
|
-
## API Reference
|
|
45
|
-
|
|
46
|
-
### Types
|
|
47
|
-
|
|
48
|
-
#### `ApiClient`
|
|
49
|
-
The main API client service interface.
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
interface ApiClient extends InitializedService {
|
|
53
|
-
handler: ModuleHandler
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Factory Functions
|
|
58
|
-
|
|
59
|
-
#### `createApiService(alias?: string): ApiClient`
|
|
60
|
-
Creates a new API client service.
|
|
61
|
-
|
|
62
|
-
**Parameters:**
|
|
63
|
-
- `alias` (optional): Service alias for registration (defaults to `'web-client'`)
|
|
64
|
-
|
|
65
|
-
**Returns:** Configured ApiClient instance
|
|
66
|
-
|
|
67
|
-
**Example:**
|
|
68
|
-
```typescript
|
|
69
|
-
import { createApiService } from '@owlmeans/api'
|
|
70
|
-
|
|
71
|
-
const apiClient = createApiService('my-api-client')
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
#### `appendApiClient<C, T>(ctx: T, alias?: string): T`
|
|
75
|
-
Appends an API client service to an existing context.
|
|
76
|
-
|
|
77
|
-
**Parameters:**
|
|
78
|
-
- `ctx`: OwlMeans context instance
|
|
79
|
-
- `alias` (optional): Service alias (defaults to `'web-client'`)
|
|
80
|
-
|
|
81
|
-
**Returns:** The context with the API client registered
|
|
82
|
-
|
|
83
|
-
**Example:**
|
|
84
|
-
```typescript
|
|
85
|
-
import { appendApiClient } from '@owlmeans/api'
|
|
86
|
-
import { createBasicContext } from '@owlmeans/context'
|
|
87
|
-
|
|
88
|
-
const context = createBasicContext()
|
|
89
|
-
appendApiClient(context, 'api-service')
|
|
15
|
+
bun add @owlmeans/api
|
|
90
16
|
```
|
|
91
17
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
#### HTTP Status Codes
|
|
95
|
-
```typescript
|
|
96
|
-
const OK = 200
|
|
97
|
-
const CREATED = 201
|
|
98
|
-
const ACCEPTED = 202
|
|
99
|
-
const FINISHED = 204
|
|
100
|
-
const UNAUTHORIZED_ERROR = 401
|
|
101
|
-
const FORBIDDEN_ERROR = 403
|
|
102
|
-
const SERVER_ERROR = 500
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
#### Default Configuration
|
|
106
|
-
```typescript
|
|
107
|
-
const DEFAULT_ALIAS = 'web-client'
|
|
108
|
-
const protocols = ['http', 'https', 'wss', 'ws']
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### Error Types
|
|
112
|
-
|
|
113
|
-
The package provides a comprehensive error hierarchy for API-related failures:
|
|
114
|
-
|
|
115
|
-
#### `ApiError`
|
|
116
|
-
Base error class for all API-related errors.
|
|
18
|
+
## Usage
|
|
117
19
|
|
|
118
|
-
|
|
119
|
-
General client-side API errors.
|
|
120
|
-
|
|
121
|
-
#### `ServerCrashedError`
|
|
122
|
-
Error indicating server-side crashes (500 errors).
|
|
123
|
-
|
|
124
|
-
#### `ServerAuthError`
|
|
125
|
-
Error for authentication failures (401 errors).
|
|
126
|
-
|
|
127
|
-
## Usage Examples
|
|
128
|
-
|
|
129
|
-
### Basic Setup
|
|
20
|
+
The API client is registered automatically by `@owlmeans/web-client`'s `makeContext`. Direct use is only needed for custom setups:
|
|
130
21
|
|
|
131
22
|
```typescript
|
|
132
|
-
import { createBasicContext } from '@owlmeans/context'
|
|
133
23
|
import { appendApiClient } from '@owlmeans/api'
|
|
134
24
|
|
|
135
|
-
|
|
136
|
-
const context = createBasicContext({
|
|
137
|
-
webService: 'api-client'
|
|
138
|
-
})
|
|
139
|
-
appendApiClient(context, 'api-client')
|
|
140
|
-
|
|
141
|
-
// Initialize the context
|
|
142
|
-
await context.init()
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### Making API Calls through Modules
|
|
146
|
-
|
|
147
|
-
```typescript
|
|
148
|
-
import { createApiService } from '@owlmeans/api'
|
|
149
|
-
|
|
150
|
-
// Get the API client from context
|
|
151
|
-
const apiClient = context.service('api-client')
|
|
152
|
-
|
|
153
|
-
// Use with a module - the handler processes the request
|
|
154
|
-
const request = {
|
|
155
|
-
alias: 'user-profile',
|
|
156
|
-
params: { userId: '123' },
|
|
157
|
-
query: { include: 'permissions' },
|
|
158
|
-
headers: { 'Accept': 'application/json' }
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const [result, outcome] = await apiClient.handler(request, responseHandler)
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Error Handling
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
import { ApiError, ServerCrashedError, ServerAuthError } from '@owlmeans/api'
|
|
168
|
-
|
|
169
|
-
try {
|
|
170
|
-
const [result, outcome] = await apiClient.handler(request, response)
|
|
171
|
-
console.log('API call successful:', result)
|
|
172
|
-
} catch (error) {
|
|
173
|
-
if (error instanceof ServerCrashedError) {
|
|
174
|
-
console.error('Server crashed:', error.message)
|
|
175
|
-
} else if (error instanceof ServerAuthError) {
|
|
176
|
-
console.error('Authentication failed:', error.message)
|
|
177
|
-
// Redirect to login
|
|
178
|
-
} else if (error instanceof ApiError) {
|
|
179
|
-
console.error('API error:', error.message)
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
### Custom Configuration
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
import { createApiService } from '@owlmeans/api'
|
|
188
|
-
import { createBasicContext } from '@owlmeans/context'
|
|
189
|
-
|
|
190
|
-
const context = createBasicContext({
|
|
191
|
-
webService: 'custom-api',
|
|
192
|
-
// Configure base URLs, security settings, etc.
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
const apiClient = createApiService('custom-api')
|
|
196
|
-
context.registerService(apiClient)
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
## Advanced Features
|
|
200
|
-
|
|
201
|
-
### Automatic Token Refresh
|
|
202
|
-
|
|
203
|
-
The API client automatically handles authentication token updates:
|
|
204
|
-
|
|
205
|
-
```typescript
|
|
206
|
-
// When server responds with TOKEN_UPDATE header,
|
|
207
|
-
// the client automatically updates the auth service
|
|
208
|
-
const response = await apiClient.handler(request, reply)
|
|
209
|
-
// Token is updated in background if provided by server
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
### Request Transformation
|
|
213
|
-
|
|
214
|
-
The client handles various content types and request transformations:
|
|
215
|
-
|
|
216
|
-
```typescript
|
|
217
|
-
// Automatic form encoding for application/x-www-form-urlencoded
|
|
218
|
-
const request = {
|
|
219
|
-
alias: 'form-submit',
|
|
220
|
-
body: { name: 'John', email: 'john@example.com' },
|
|
221
|
-
headers: { 'content-type': 'application/x-www-form-urlencoded' }
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Automatic JSON handling
|
|
225
|
-
const jsonRequest = {
|
|
226
|
-
alias: 'api-call',
|
|
227
|
-
body: JSON.stringify({ data: 'value' }),
|
|
228
|
-
headers: { 'content-type': 'application/json' }
|
|
229
|
-
}
|
|
25
|
+
appendApiClient(context)
|
|
230
26
|
```
|
|
231
27
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
URLs are automatically constructed from module route definitions:
|
|
28
|
+
HTTP status constants:
|
|
235
29
|
|
|
236
30
|
```typescript
|
|
237
|
-
|
|
238
|
-
// Request provides: { userId: '123' }
|
|
239
|
-
// Result: /users/123/profile
|
|
240
|
-
|
|
241
|
-
const request = {
|
|
242
|
-
alias: 'user-profile',
|
|
243
|
-
params: { userId: '123' },
|
|
244
|
-
host: 'api.example.com',
|
|
245
|
-
base: '/v1'
|
|
246
|
-
}
|
|
247
|
-
// Final URL: https://api.example.com/v1/users/123/profile
|
|
31
|
+
import { OK, CREATED, UNAUTHORIZED_ERROR, NOT_FOUND_ERROR } from '@owlmeans/api'
|
|
248
32
|
```
|
|
249
33
|
|
|
250
|
-
|
|
34
|
+
## API
|
|
251
35
|
|
|
252
|
-
|
|
36
|
+
### `createApiService(alias?): ApiClient`
|
|
253
37
|
|
|
254
|
-
|
|
255
|
-
// Success responses (200, 201, 202, 204)
|
|
256
|
-
// Error responses (401, 403, 500, etc.)
|
|
257
|
-
// Custom error marshaling from server
|
|
258
|
-
// Header-based responses for empty bodies
|
|
259
|
-
```
|
|
38
|
+
Creates an HTTP client service. `alias` defaults to `DEFAULT_ALIAS` (`'web-client'`).
|
|
260
39
|
|
|
261
|
-
|
|
40
|
+
### `appendApiClient<C, T>(ctx, alias?): T`
|
|
262
41
|
|
|
263
|
-
|
|
42
|
+
Registers the API client in the context.
|
|
264
43
|
|
|
265
|
-
|
|
266
|
-
- **@owlmeans/module**: Module-based request/response handling
|
|
267
|
-
- **@owlmeans/client-route**: URL parameter extraction and route processing
|
|
268
|
-
- **@owlmeans/auth-common**: Automatic authentication integration
|
|
269
|
-
- **@owlmeans/config**: Security helpers and URL construction
|
|
270
|
-
- **@owlmeans/error**: Resilient error handling and marshaling
|
|
271
|
-
- **@owlmeans/client-config**: Configuration management
|
|
44
|
+
### Error Classes
|
|
272
45
|
|
|
273
|
-
|
|
46
|
+
- `ApiError` — base API error
|
|
47
|
+
- `ApiClientError` — client-side request error
|
|
48
|
+
- `ServerCrashedError` — 5xx response
|
|
49
|
+
- `ServerAuthError` — 401/403 response
|
|
274
50
|
|
|
275
|
-
|
|
276
|
-
- Authentication tokens are automatically managed
|
|
277
|
-
- Request/response data should be validated
|
|
278
|
-
- Error responses may contain sensitive information and should be handled appropriately
|
|
279
|
-
- CORS and CSP headers should be configured properly on the server side
|
|
51
|
+
### Constants
|
|
280
52
|
|
|
281
|
-
|
|
53
|
+
`DEFAULT_ALIAS`, `OK`, `CREATED`, `UNAUTHORIZED_ERROR`, `NOT_FOUND_ERROR`, `SERVER_ERROR`
|
|
282
54
|
|
|
283
|
-
|
|
284
|
-
- Efficient parameter extraction and URL construction
|
|
285
|
-
- Minimal overhead for authenticated requests
|
|
286
|
-
- Built-in retry logic through the OwlMeans module system
|
|
55
|
+
## Related Packages
|
|
287
56
|
|
|
288
|
-
|
|
57
|
+
- [`@owlmeans/client-module`](../client-module) — `ClientModule<T>` uses this service to make requests
|
|
58
|
+
- [`@owlmeans/web-client`](../web-client) — registers this service via `makeContext`
|
package/build/service.js
CHANGED
|
@@ -15,7 +15,7 @@ export const createApiService = (alias = DEFAULT_ALIAS) => {
|
|
|
15
15
|
return;
|
|
16
16
|
}
|
|
17
17
|
const context = assertContext(client.ctx, location);
|
|
18
|
-
const module = context.
|
|
18
|
+
const module = context.entrypoint(request.alias);
|
|
19
19
|
await module.resolve();
|
|
20
20
|
const route = module.route.route;
|
|
21
21
|
let path = module.getPath();
|
package/build/service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEhE,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAEtD,OAAO,KAAuB,MAAM,OAAO,CAAA;AAG3C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEpD,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,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'@owlmeans/context';
|
|
2
|
-
import type {
|
|
2
|
+
import type { EntrypointHandler } from '@owlmeans/entrypoint';
|
|
3
3
|
export interface ApiClient extends InitializedService {
|
|
4
|
-
handler:
|
|
4
|
+
handler: EntrypointHandler;
|
|
5
5
|
}
|
|
6
6
|
//# sourceMappingURL=types.d.ts.map
|
package/build/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAE7D,MAAM,WAAW,SAAU,SAAQ,kBAAkB;IACnD,OAAO,EAAE,iBAAiB,CAAA;CAC3B"}
|
package/build/utils/handler.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AbstractResponse } from '@owlmeans/
|
|
1
|
+
import type { AbstractResponse } from '@owlmeans/entrypoint';
|
|
2
2
|
import type { AxiosResponse } from 'axios';
|
|
3
3
|
export declare const processResponse: (response: AxiosResponse, reply: AbstractResponse<any>) => void;
|
|
4
4
|
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/utils/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/utils/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAK1C,eAAO,MAAM,eAAe,GAAI,UAAU,aAAa,EAAE,OAAO,gBAAgB,CAAC,GAAG,CAAC,SAyCpF,CAAA"}
|
package/build/utils/handler.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EntrypointOutcome } from '@owlmeans/entrypoint';
|
|
2
2
|
import { ACCEPTED, CREATED, FINISHED, FORBIDDEN_ERROR, OK, SERVER_ERROR, UNAUTHORIZED_ERROR } from '../consts.js';
|
|
3
3
|
import { ResilientError } from '@owlmeans/error';
|
|
4
4
|
import { ApiClientError, ServerAuthError, ServerCrashedError } from '../errors.js';
|
|
5
5
|
export const processResponse = (response, reply) => {
|
|
6
6
|
switch (response.status) {
|
|
7
7
|
case OK:
|
|
8
|
-
reply.resolve(response.data,
|
|
8
|
+
reply.resolve(response.data, EntrypointOutcome.Ok);
|
|
9
9
|
return;
|
|
10
10
|
case ACCEPTED:
|
|
11
|
-
reply.resolve(response.data,
|
|
11
|
+
reply.resolve(response.data, EntrypointOutcome.Accepted);
|
|
12
12
|
return;
|
|
13
13
|
case CREATED:
|
|
14
|
-
return processEmptyResponse(response, reply,
|
|
14
|
+
return processEmptyResponse(response, reply, EntrypointOutcome.Created);
|
|
15
15
|
case FINISHED:
|
|
16
|
-
return processEmptyResponse(response, reply,
|
|
16
|
+
return processEmptyResponse(response, reply, EntrypointOutcome.Finished);
|
|
17
17
|
default:
|
|
18
18
|
console.error(response.status, response.statusText);
|
|
19
19
|
console.error(response.config.url, response.headers);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/utils/handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/utils/handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,C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EAAE,OAAO,CAAC,CAAA;AAC1C,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/api",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"license": "MIT",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"scripts": {
|
|
6
7
|
"build": "tsc -b",
|
|
@@ -20,22 +21,23 @@
|
|
|
20
21
|
}
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@owlmeans/auth-common": "^0.1.
|
|
24
|
-
"@owlmeans/client-config": "^0.1.
|
|
25
|
-
"@owlmeans/client-route": "^0.1.
|
|
26
|
-
"@owlmeans/config": "^0.1.
|
|
27
|
-
"@owlmeans/context": "^0.1.
|
|
28
|
-
"@owlmeans/error": "^0.1.
|
|
29
|
-
"@owlmeans/
|
|
30
|
-
"@owlmeans/route": "^0.1.
|
|
24
|
+
"@owlmeans/auth-common": "^0.1.3",
|
|
25
|
+
"@owlmeans/client-config": "^0.1.3",
|
|
26
|
+
"@owlmeans/client-route": "^0.1.3",
|
|
27
|
+
"@owlmeans/config": "^0.1.3",
|
|
28
|
+
"@owlmeans/context": "^0.1.3",
|
|
29
|
+
"@owlmeans/error": "^0.1.3",
|
|
30
|
+
"@owlmeans/entrypoint": "^0.1.3",
|
|
31
|
+
"@owlmeans/route": "^0.1.3",
|
|
31
32
|
"axios": "^1.7.7",
|
|
32
33
|
"qs": "^6.13.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
36
|
+
"@owlmeans/dep-config": "workspace:*",
|
|
35
37
|
"@types/qs": "^6.9.16",
|
|
36
38
|
"nodemon": "^3.1.11",
|
|
37
39
|
"npm-check": "^6.0.1",
|
|
38
|
-
"typescript": "^
|
|
40
|
+
"typescript": "^6.0.2"
|
|
39
41
|
},
|
|
40
42
|
"publishConfig": {
|
|
41
43
|
"access": "public"
|
package/src/service.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { extractParams } from '@owlmeans/client-route'
|
|
|
4
4
|
import type { ApiClient } from './types.js'
|
|
5
5
|
import axios, { AxiosHeaders } from 'axios'
|
|
6
6
|
import type { AxiosRequestTransformer } from 'axios'
|
|
7
|
-
import type {
|
|
7
|
+
import type { CommonEntrypoint } from '@owlmeans/entrypoint'
|
|
8
8
|
import { DEFAULT_ALIAS } from './consts.js'
|
|
9
9
|
import { processResponse } from './utils/handler.js'
|
|
10
10
|
import { BasicClientConfig } from '@owlmeans/client-config'
|
|
@@ -27,7 +27,7 @@ export const createApiService = (alias: string = DEFAULT_ALIAS): ApiClient => {
|
|
|
27
27
|
return
|
|
28
28
|
}
|
|
29
29
|
const context = assertContext<Config, Context>(client.ctx, location)
|
|
30
|
-
const module = context.
|
|
30
|
+
const module = context.entrypoint<CommonEntrypoint>(request.alias)
|
|
31
31
|
await module.resolve()
|
|
32
32
|
const route = module.route.route
|
|
33
33
|
let path = module.getPath()
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { InitializedService } from '@owlmeans/context'
|
|
2
|
-
import type {
|
|
2
|
+
import type { EntrypointHandler } from '@owlmeans/entrypoint'
|
|
3
3
|
|
|
4
4
|
export interface ApiClient extends InitializedService {
|
|
5
|
-
handler:
|
|
5
|
+
handler: EntrypointHandler
|
|
6
6
|
}
|
package/src/utils/handler.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AbstractResponse } from '@owlmeans/
|
|
2
|
-
import {
|
|
1
|
+
import type { AbstractResponse } from '@owlmeans/entrypoint'
|
|
2
|
+
import { EntrypointOutcome } from '@owlmeans/entrypoint'
|
|
3
3
|
import type { AxiosResponse } from 'axios'
|
|
4
4
|
import { ACCEPTED, CREATED, FINISHED, FORBIDDEN_ERROR, OK, SERVER_ERROR, UNAUTHORIZED_ERROR } from '../consts.js'
|
|
5
5
|
import { ResilientError } from '@owlmeans/error'
|
|
@@ -8,15 +8,15 @@ import { ApiClientError, ServerAuthError, ServerCrashedError } from '../errors.j
|
|
|
8
8
|
export const processResponse = (response: AxiosResponse, reply: AbstractResponse<any>) => {
|
|
9
9
|
switch (response.status) {
|
|
10
10
|
case OK:
|
|
11
|
-
reply.resolve(response.data,
|
|
11
|
+
reply.resolve(response.data, EntrypointOutcome.Ok)
|
|
12
12
|
return
|
|
13
13
|
case ACCEPTED:
|
|
14
|
-
reply.resolve(response.data,
|
|
14
|
+
reply.resolve(response.data, EntrypointOutcome.Accepted)
|
|
15
15
|
return
|
|
16
16
|
case CREATED:
|
|
17
|
-
return processEmptyResponse(response, reply,
|
|
17
|
+
return processEmptyResponse(response, reply, EntrypointOutcome.Created)
|
|
18
18
|
case FINISHED:
|
|
19
|
-
return processEmptyResponse(response, reply,
|
|
19
|
+
return processEmptyResponse(response, reply, EntrypointOutcome.Finished)
|
|
20
20
|
default:
|
|
21
21
|
|
|
22
22
|
console.error(response.status, response.statusText)
|
|
@@ -48,7 +48,7 @@ export const processResponse = (response: AxiosResponse, reply: AbstractResponse
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
const processEmptyResponse = (response: AxiosResponse, reply: AbstractResponse<any>, outcome:
|
|
51
|
+
const processEmptyResponse = (response: AxiosResponse, reply: AbstractResponse<any>, outcome: EntrypointOutcome) => {
|
|
52
52
|
if (response.data != null && response.data != '') {
|
|
53
53
|
reply.resolve(response.data, outcome)
|
|
54
54
|
return
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": [
|
|
3
|
-
"
|
|
3
|
+
"@owlmeans/dep-config/tsconfig.base.json"
|
|
4
4
|
],
|
|
5
5
|
"compilerOptions": {
|
|
6
|
-
"rootDir": "./src/",
|
|
7
|
-
"outDir": "./build/"
|
|
6
|
+
"rootDir": "./src/",
|
|
7
|
+
"outDir": "./build/"
|
|
8
8
|
},
|
|
9
|
-
"exclude": [
|
|
10
|
-
|
|
11
|
-
"./build/**/*",
|
|
12
|
-
"./*.ts"
|
|
13
|
-
]
|
|
14
|
-
}
|
|
9
|
+
"exclude": ["./dist/**/*", "./build/**/*", "./*.ts"]
|
|
10
|
+
}
|