@mondart/nestjs-common-module-request 3.1.5 → 3.1.7
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
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @mondart/nestjs-common-module-request
|
|
2
|
+
|
|
3
|
+
A small HTTP client wrapper for NestJS: a `RequestModule` that registers a
|
|
4
|
+
global `RequestService` built on `@nestjs/axios`, for calling other HTTP
|
|
5
|
+
services with a bearer token attached.
|
|
6
|
+
|
|
7
|
+
## Registration
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { RequestModule } from '@mondart/nestjs-common-module-request';
|
|
11
|
+
|
|
12
|
+
@Module({
|
|
13
|
+
imports: [RequestModule],
|
|
14
|
+
})
|
|
15
|
+
export class AppModule {}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`RequestModule` is `@Global()`, so `RequestService` is available for
|
|
19
|
+
injection anywhere without re-importing the module.
|
|
20
|
+
|
|
21
|
+
## RequestService
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { RequestService, HttpMethods } from '@mondart/nestjs-common-module-request';
|
|
25
|
+
|
|
26
|
+
constructor(private readonly requestService: RequestService) {}
|
|
27
|
+
|
|
28
|
+
const user = await this.requestService.get({
|
|
29
|
+
path: 'https://api.example.com/users/1',
|
|
30
|
+
token: accessToken,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await this.requestService.post({
|
|
34
|
+
path: 'https://api.example.com/users',
|
|
35
|
+
body: { name: 'Ada' },
|
|
36
|
+
token: accessToken,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
await this.requestService.patch({ path: '...', body: { name: 'Ada L.' } });
|
|
40
|
+
await this.requestService.remove({ path: 'https://api.example.com/users/1' });
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`post`/`get`/`patch`/`remove` are convenience wrappers around `send` with
|
|
44
|
+
`method` fixed to the matching `HttpMethods` value. `path` is used as the
|
|
45
|
+
full request URL, `token` (if given) is sent as `Authorization: Bearer
|
|
46
|
+
<token>`, and `params`, if given, is appended to `path` as-is (e.g.
|
|
47
|
+
`'foo=1&bar=2'`) rather than being serialized from an object.
|
|
48
|
+
|
|
49
|
+
`send` resolves to `result.data` on success. On failure it does **not**
|
|
50
|
+
throw — the caught error is returned as the resolved value instead, so
|
|
51
|
+
callers must check the result shape rather than relying on a rejected
|
|
52
|
+
promise or a try/catch.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const result = await this.requestService.get({ path: '...' });
|
|
56
|
+
if (result instanceof Error) {
|
|
57
|
+
// handle failure
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## sendRaw
|
|
62
|
+
|
|
63
|
+
`sendRaw` returns the full Axios response instead of just the body, and
|
|
64
|
+
supports resolving `path` against a configured base URL:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
await this.requestService.sendRaw({
|
|
68
|
+
method: HttpMethods.GET,
|
|
69
|
+
path: '/users/1',
|
|
70
|
+
isAdditionalPath: true, // path is appended to config `app.baseUrl`
|
|
71
|
+
token: accessToken,
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
When `isAdditionalPath` is `true`, the request URL is
|
|
76
|
+
`configService.get('app.baseUrl') + path`; otherwise `path` is used as the
|
|
77
|
+
full URL, same as `send`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.module.d.ts","sourceRoot":"","sources":["../src/request.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request.module.d.ts","sourceRoot":"","sources":["../src/request.module.ts"],"names":[],"mappings":"AASA,qBAMa,aAAa;CAAG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.module.js","sourceRoot":"","sources":["../src/request.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,yCAA2C;AAC3C,2CAAgD;AAChD,uDAAmD;
|
|
1
|
+
{"version":3,"file":"request.module.js","sourceRoot":"","sources":["../src/request.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,yCAA2C;AAC3C,2CAAgD;AAChD,uDAAmD;AAa5C,IAAM,aAAa,GAAnB,MAAM,aAAa;CAAG,CAAA;AAAhB,sCAAa;wBAAb,aAAa;IANzB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,kBAAU,CAAC;QACrB,SAAS,EAAE,CAAC,gCAAc,CAAC;QAC3B,OAAO,EAAE,CAAC,gCAAc,CAAC;KAC1B,CAAC;GACW,aAAa,CAAG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.service.d.ts","sourceRoot":"","sources":["../src/request.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C,UAAU,YAAY;IACpB,MAAM,EAAE,WAAW,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,UAAU,WAAW;IACnB,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;
|
|
1
|
+
{"version":3,"file":"request.service.d.ts","sourceRoot":"","sources":["../src/request.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,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1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.service.js","sourceRoot":"","sources":["../src/request.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAA4C;AAC5C,2CAA4C;AAC5C,+BAAsC;AACtC,iEAAwD;
|
|
1
|
+
{"version":3,"file":"request.service.js","sourceRoot":"","sources":["../src/request.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAA4C;AAC5C,2CAA4C;AAC5C,+BAAsC;AACtC,iEAAwD;AAuH/C,4FAvHA,+BAAW,OAuHA;AAtHpB,2CAA+C;AAyBxC,IAAM,cAAc,GAApB,MAAM,cAAc;IACzB,YACmB,aAA4B,EAC5B,WAAwB;QADxB,kBAAa,GAAb,aAAa,CAAe;QAC5B,gBAAW,GAAX,WAAW,CAAa;IACxC,CAAC;IAOJ,KAAK,CAAC,OAAO,CAAC,EACZ,MAAM,EACN,IAAI,EACJ,gBAAgB,EAChB,IAAI,EACJ,OAAO,EACP,KAAK,GACQ;QACb,MAAM,GAAG,GAAG,gBAAgB;YAC1B,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,aAAa,CAAC,GAAG,IAAI;YAC9C,CAAC,CAAC,IAAI,CAAC;QAET,MAAM,MAAM,GAAG,MAAM,IAAA,qBAAc,EACjC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAI;YAC1B,MAAM;YACN,IAAI,EAAE,IAAI,IAAI,EAAE;YAChB,GAAG,EAAE,GAAG;YACR,OAAO,EAAE;gBACP,GAAG,OAAO;gBACV,aAAa,EAAE,UAAU,KAAK,EAAE;aACjC;SACF,CAAC,CACH,CAAC;QAEF,OAAO,MAAM,CAAC;IAChB,CAAC;IAQD,KAAK,CAAC,IAAI,CAAC,EACT,MAAM,EACN,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,KAAK,EACL,MAAM,GACO;QACb,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,GAAG,IAAI,MA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
2
|
"name": "@mondart/nestjs-common-module-request",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.7",
|
|
4
4
|
"description": "HTTP request module and service for NestJS.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Mondart"
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"build": "tsc -p tsconfig.json"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@mondart/nestjs-common-module-core": "^3.2.
|
|
20
|
+
"@mondart/nestjs-common-module-core": "^3.2.6",
|
|
21
21
|
"@nestjs/axios": "^4.0.0",
|
|
22
22
|
"@nestjs/config": "^4.0.0",
|
|
23
23
|
"axios": "~1.15.0"
|