@furystack/rest-service 10.0.19 → 10.0.21
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 +18 -18
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# rest-service
|
|
2
2
|
|
|
3
|
-
REST
|
|
3
|
+
REST service (implementation) package for `@furystack/rest`.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Start by importing your custom API endpoint interface (see `@furystack/rest`) and use the `.useRestService<MyApi>(...)` injector extension method. You can define multiple REST services per injector (even on the same port).
|
|
8
8
|
|
|
9
|
-
### Implementing a
|
|
9
|
+
### Implementing a Custom API
|
|
10
10
|
|
|
11
|
-
Usage example
|
|
11
|
+
Usage example – authenticated GET, GET collection, and POST APIs for a custom entity that has a physical store and repository set up:
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
import { MyApi, MyEntity } from 'my-common-package'
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
} from '@furystack/rest-service'
|
|
21
21
|
|
|
22
22
|
myInjector.useHttpAuthentication().useRestService<MyApi>({
|
|
23
|
-
port: 8080, // The port to listen
|
|
23
|
+
port: 8080, // The port to listen on
|
|
24
24
|
root: '/api', // Routes will be joined on this root path
|
|
25
25
|
cors: {
|
|
26
26
|
// Enable CORS
|
|
@@ -29,12 +29,12 @@ myInjector.useHttpAuthentication().useRestService<MyApi>({
|
|
|
29
29
|
},
|
|
30
30
|
// This API should implement *all* methods that are defined in `MyApi`
|
|
31
31
|
api: {
|
|
32
|
-
// Endpoints that can be called with GET
|
|
32
|
+
// Endpoints that can be called with GET HTTP method
|
|
33
33
|
GET: {
|
|
34
34
|
'/my-entities': Authenticate()(createGetCollectionEndpoint({ model: MyEntity, primaryKey: 'id' })),
|
|
35
35
|
'/my-entities/:id': Authenticate()(createGetEntityEndpoint({ model: MyEntity, primaryKey: 'id' })),
|
|
36
36
|
},
|
|
37
|
-
// Endpoints that can be called with
|
|
37
|
+
// Endpoints that can be called with POST HTTP method
|
|
38
38
|
POST: {
|
|
39
39
|
'/my-entities': Authenticate()(createPostEndpoint({ model: MyEntity, primaryKey: 'id' })),
|
|
40
40
|
},
|
|
@@ -42,9 +42,9 @@ myInjector.useHttpAuthentication().useRestService<MyApi>({
|
|
|
42
42
|
})
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
### Endpoint
|
|
45
|
+
### Endpoint Generators (Based on Repository DataSets)
|
|
46
46
|
|
|
47
|
-
If you use the underlying layers of FuryStack (`PhysicalStore` -> `Repository`) for an entity type, you can easily create some CRUD endpoints for them. These
|
|
47
|
+
If you use the underlying layers of FuryStack (`PhysicalStore` -> `Repository`) for an entity type, you can easily create some CRUD endpoints for them. These include:
|
|
48
48
|
|
|
49
49
|
- createDeleteEndpoint()
|
|
50
50
|
- createGetCollectionEndpoint()
|
|
@@ -54,9 +54,9 @@ If you use the underlying layers of FuryStack (`PhysicalStore` -> `Repository`)
|
|
|
54
54
|
|
|
55
55
|
The endpoints will use the defined Physical Stores for retrieving entities and the Repository for authorization / event subscriptions.
|
|
56
56
|
|
|
57
|
-
### Custom
|
|
57
|
+
### Custom Endpoint Implementation
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
To implement an endpoint with custom logic, define it as follows:
|
|
60
60
|
|
|
61
61
|
```ts
|
|
62
62
|
import { Injector } from '@furystack/inject'
|
|
@@ -168,13 +168,13 @@ getResult().then((data) => {
|
|
|
168
168
|
})
|
|
169
169
|
```
|
|
170
170
|
|
|
171
|
-
### Payload
|
|
171
|
+
### Payload Validation
|
|
172
172
|
|
|
173
|
-
Type-safe APIs
|
|
174
|
-
The
|
|
173
|
+
Type-safe APIs do **NOT** come with built-in validation by default - but you can use the JSON Schema for full payload validation.
|
|
174
|
+
The preferred way is:
|
|
175
175
|
|
|
176
176
|
1. Create your API interface
|
|
177
|
-
1. Create JSON Schemas from the API (The `ts-json-schema-generator` package is the best solution
|
|
177
|
+
1. Create JSON Schemas from the API (The `ts-json-schema-generator` package is the best solution nowadays, you can check how it works, [here](https://github.com/furystack/furystack/blob/develop/package.json#L39))
|
|
178
178
|
1. Use the Validate middleware, as shown in the following example:
|
|
179
179
|
|
|
180
180
|
```ts
|
|
@@ -190,7 +190,7 @@ In that way, you will get full validation for _all_ defined endpoint data (heade
|
|
|
190
190
|
|
|
191
191
|
### Authentication and HttpUserContext
|
|
192
192
|
|
|
193
|
-
You can use the
|
|
193
|
+
You can use the built-in authentication that comes with this package. It contains a session (~cookie) based authentication and Basic Auth. You can use it with the `.useCommonAuth()` injector extension:
|
|
194
194
|
|
|
195
195
|
```ts
|
|
196
196
|
myInjector.useCommonAuth({{
|
|
@@ -203,9 +203,9 @@ myInjector.useCommonAuth({{
|
|
|
203
203
|
}).useRestService<MyApi>({...api options})
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
-
### Built-in
|
|
206
|
+
### Built-in Actions
|
|
207
207
|
|
|
208
|
-
The package contains the following built-in actions
|
|
208
|
+
The package contains the following built-in actions:
|
|
209
209
|
|
|
210
210
|
- `ErrorAction` - for default error handling and dumping errors in the response
|
|
211
211
|
- `GetCurrentUser` - Returns the current user
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@furystack/rest-service",
|
|
3
|
-
"version": "10.0.
|
|
3
|
+
"version": "10.0.21",
|
|
4
4
|
"description": "Repository implementation for FuryStack",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -37,22 +37,22 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/furystack/furystack",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@furystack/core": "^15.0.
|
|
41
|
-
"@furystack/inject": "^12.0.
|
|
42
|
-
"@furystack/repository": "^10.0.
|
|
43
|
-
"@furystack/rest": "^8.0.
|
|
44
|
-
"@furystack/security": "^6.0.
|
|
45
|
-
"@furystack/utils": "^8.0.
|
|
40
|
+
"@furystack/core": "^15.0.20",
|
|
41
|
+
"@furystack/inject": "^12.0.17",
|
|
42
|
+
"@furystack/repository": "^10.0.20",
|
|
43
|
+
"@furystack/rest": "^8.0.20",
|
|
44
|
+
"@furystack/security": "^6.0.20",
|
|
45
|
+
"@furystack/utils": "^8.0.16",
|
|
46
46
|
"ajv": "^8.17.1",
|
|
47
47
|
"ajv-formats": "^3.0.1",
|
|
48
48
|
"path-to-regexp": "^8.2.0",
|
|
49
49
|
"semaphore-async-await": "^1.5.1"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@furystack/rest-client-fetch": "^8.0.
|
|
53
|
-
"@types/node": "^
|
|
52
|
+
"@furystack/rest-client-fetch": "^8.0.20",
|
|
53
|
+
"@types/node": "^24.0.1",
|
|
54
54
|
"typescript": "^5.8.3",
|
|
55
|
-
"vitest": "^3.
|
|
55
|
+
"vitest": "^3.2.3"
|
|
56
56
|
},
|
|
57
57
|
"gitHead": "1045d854bfd8c475b7035471d130d401417a2321"
|
|
58
58
|
}
|