@nestia/fetcher 1.4.0 → 1.5.0-dev.20230802

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 CHANGED
@@ -11,21 +11,27 @@ Nestia is a set of helper libraries for NestJS, supporting below features:
11
11
 
12
12
  - `@nestia/core`: super-fast decorators
13
13
  - `@nestia/sdk`:
14
- - SDK generator for clients
15
14
  - Swagger generator evolved than ever
15
+ - SDK library generator for clients
16
+ - Mockup Simulator for client applications
16
17
  - Automatic E2E test functions generator
18
+ - `@nestia/migrate`: migration from Swagger to NestJS
17
19
  - `nestia`: just CLI (command line interface) tool
18
20
 
19
21
  > **Note**
20
22
  >
21
23
  > - **Only one line** required, with pure TypeScript type
22
- > - Runtime validator is **20,000x faster** than `class-validator`
23
- > - JSON serialization is **200x faster** than `class-transformer`
24
- > - SDK is similar with [tRPC](https://trpc.io), but much advanced
24
+ > - Enhance performance **30x** up
25
+ > - Runtime validator is **20,000x faster** than `class-validator`
26
+ > - JSON serialization is **200x faster** than `class-transformer`
27
+ > - Software Development Kit
28
+ > - SDK is a collection of `fetch` functions with type definitions like [tRPC](https://trpc.io/)
29
+ > - Mockup simulator means embedded backend simulator in SDK
30
+ > - similar with [msw](https://mswjs.io/), but fully automated
25
31
 
26
32
  ![nestia-sdk-demo](https://user-images.githubusercontent.com/13158709/215004990-368c589d-7101-404e-b81b-fbc936382f05.gif)
27
33
 
28
- > Left is server code, and right is client code utilizing SDK
34
+ > Left is NestJS server code, and right is client (frontend) code utilizing SDK
29
35
 
30
36
 
31
37
 
@@ -46,9 +52,9 @@ Check out the document in the [website](https://nestia.io/docs/):
46
52
  ### 🏠 Home
47
53
  - [Introduction](https://nestia.io/docs/)
48
54
  - [Setup](https://nestia.io/docs/setup/)
55
+ - [Pure TypeScript](https://nestia.io/docs/pure)
49
56
 
50
57
  ### 📖 Features
51
- - [Pure TypeScript](https://nestia.io/docs/pure)
52
58
  - Core Library
53
59
  - [TypedRoute](https://nestia.io/docs/core/TypedRoute/)
54
60
  - [TypedBody](https://nestia.io/docs/core/TypedBody/)
@@ -58,3 +64,9 @@ Check out the document in the [website](https://nestia.io/docs/):
58
64
  - [Swagger Documents](https://nestia.io/docs/sdk/swagger/)
59
65
  - [SDK Library](https://nestia.io/docs/sdk/sdk/)
60
66
  - [E2E Functions](https://nestia.io/docs/sdk/e2e/)
67
+ - [Mockup Simulator](https://nestia.io/docs/sdk/simulator/)
68
+ - [Swagger to NestJS](https://nestia.io/docs/migrate/)
69
+
70
+ ### 🔗 Appendix
71
+ - [⇲ Benchmark Result](https://github.com/samchon/nestia/tree/master/benchmark/results/11th%20Gen%20Intel(R)%20Core(TM)%20i5-1135G7%20%40%202.40GHz)
72
+ - [⇲ `dev.to` Articles](https://dev.to/samchon/series/22751)
package/lib/Fetcher.js CHANGED
@@ -46,6 +46,33 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
48
  };
49
+ var __values = (this && this.__values) || function(o) {
50
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
51
+ if (m) return m.call(o);
52
+ if (o && typeof o.length === "number") return {
53
+ next: function () {
54
+ if (o && i >= o.length) o = void 0;
55
+ return { value: o && o[i++], done: !o };
56
+ }
57
+ };
58
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
59
+ };
60
+ var __read = (this && this.__read) || function (o, n) {
61
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
62
+ if (!m) return o;
63
+ var i = m.call(o), r, ar = [], e;
64
+ try {
65
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
66
+ }
67
+ catch (error) { e = { error: error }; }
68
+ finally {
69
+ try {
70
+ if (r && !r.done && (m = i["return"])) m.call(i);
71
+ }
72
+ finally { if (e) throw e.error; }
73
+ }
74
+ return ar;
75
+ };
49
76
  var __importDefault = (this && this.__importDefault) || function (mod) {
50
77
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
78
  };
@@ -73,27 +100,62 @@ var Fetcher = /** @class */ (function () {
73
100
  function Fetcher() {
74
101
  }
75
102
  Fetcher.fetch = function (connection, encrypted, method, path, input, stringify) {
76
- var _a, _b;
103
+ var _a, _b, _c, _d, _e;
77
104
  return __awaiter(this, void 0, void 0, function () {
78
- var init, url, response, text, content, ret;
79
- return __generator(this, function (_c) {
80
- switch (_c.label) {
105
+ var headers, init, url, response, text, content, ret;
106
+ return __generator(this, function (_f) {
107
+ switch (_f.label) {
81
108
  case 0:
82
109
  if (encrypted.request === true || encrypted.response === true)
83
110
  if (connection.encryption === undefined)
84
111
  throw new Error("Error on nestia.Fetcher.encrypt(): the encryption password has not been configured.");
85
- init = __assign(__assign({}, ((_a = connection.options) !== null && _a !== void 0 ? _a : {})), { method: method, headers: encrypted.request === false &&
86
- input !== undefined &&
87
- typeof input === "object"
88
- ? __assign(__assign({}, connection.headers), { "Content-Type": "application/json" }) : (_b = connection.headers) !== null && _b !== void 0 ? _b : {} });
89
- if (encrypted.request || typeof input === "string")
90
- init.headers["Content-Type"] =
91
- "text/plain";
112
+ headers = __assign(__assign({}, ((_a = connection.headers) !== null && _a !== void 0 ? _a : {})), { "Content-Type": input !== undefined
113
+ ? ((_c = (_b = connection.headers) === null || _b === void 0 ? void 0 : _b["Content-Type"]) !== null && _c !== void 0 ? _c : (encrypted.request || typeof input === "string"))
114
+ ? "text/plain"
115
+ : "application/json"
116
+ : undefined });
117
+ init = __assign(__assign({}, ((_d = connection.options) !== null && _d !== void 0 ? _d : {})), { method: method, headers: (function () {
118
+ var e_1, _a, e_2, _b;
119
+ var output = [];
120
+ try {
121
+ for (var _c = __values(Object.entries(headers)), _d = _c.next(); !_d.done; _d = _c.next()) {
122
+ var _e = __read(_d.value, 2), key = _e[0], value = _e[1];
123
+ if (value === undefined)
124
+ continue;
125
+ else if (Array.isArray(value))
126
+ try {
127
+ for (var value_1 = (e_2 = void 0, __values(value)), value_1_1 = value_1.next(); !value_1_1.done; value_1_1 = value_1.next()) {
128
+ var v = value_1_1.value;
129
+ output.push([key, String(v)]);
130
+ }
131
+ }
132
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
133
+ finally {
134
+ try {
135
+ if (value_1_1 && !value_1_1.done && (_b = value_1.return)) _b.call(value_1);
136
+ }
137
+ finally { if (e_2) throw e_2.error; }
138
+ }
139
+ else
140
+ output.push([key, String(value)]);
141
+ }
142
+ }
143
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
144
+ finally {
145
+ try {
146
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
147
+ }
148
+ finally { if (e_1) throw e_1.error; }
149
+ }
150
+ return output;
151
+ })() });
92
152
  // REQUEST BODY (WITH ENCRYPTION)
93
153
  if (input !== undefined)
94
154
  init.body = (function () {
95
- var json = (stringify !== null && stringify !== void 0 ? stringify : JSON.stringify)(input);
96
- if (!encrypted.request)
155
+ var json = headers["Content-Type"] !== "text/plain"
156
+ ? (stringify !== null && stringify !== void 0 ? stringify : JSON.stringify)(input)
157
+ : String(input);
158
+ if (encrypted.request !== true)
97
159
  return json;
98
160
  var password = connection.encryption instanceof Function
99
161
  ? connection.encryption({
@@ -112,12 +174,12 @@ var Fetcher = /** @class */ (function () {
112
174
  path = "/" + path;
113
175
  url = new URL("".concat(connection.host).concat(path));
114
176
  return [4 /*yield*/, polyfill.get()];
115
- case 1: return [4 /*yield*/, (_c.sent())(url.href, init)];
177
+ case 1: return [4 /*yield*/, (_f.sent())(url.href, init)];
116
178
  case 2:
117
- response = _c.sent();
179
+ response = _f.sent();
118
180
  return [4 /*yield*/, response.text()];
119
181
  case 3:
120
- text = _c.sent();
182
+ text = _f.sent();
121
183
  if (!text)
122
184
  return [2 /*return*/, undefined];
123
185
  // CHECK THE STATUS CODE
@@ -141,7 +203,9 @@ var Fetcher = /** @class */ (function () {
141
203
  ret = content;
142
204
  try {
143
205
  // PARSE RESPONSE BODY
144
- ret = JSON.parse(ret);
206
+ if (encrypted.response ||
207
+ ((_e = response.headers.get("Content-Type")) !== null && _e !== void 0 ? _e : "").indexOf("application/json") !== -1)
208
+ ret = JSON.parse(ret);
145
209
  // FIND __SET_HEADERS__ FIELD
146
210
  if (ret.__set_headers__ !== undefined &&
147
211
  typeof ret.__set_headers__ === "object") {
@@ -150,7 +214,7 @@ var Fetcher = /** @class */ (function () {
150
214
  Object.assign(connection.headers, ret.__set_headers__);
151
215
  }
152
216
  }
153
- catch (_d) { }
217
+ catch (_g) { }
154
218
  // RETURNS
155
219
  return [2 /*return*/, ret];
156
220
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Fetcher.js","sourceRoot":"","sources":["../src/Fetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA8B;AAM9B,uCAAsC;AACtC,yCAAwC;AACxC,kDAAiD;AAEjD;;;;;;;;;;;;;GAaG;AACH;IAAA;IAqKA,CAAC;IAhIuB,aAAK,GAAzB,UACI,UAAuB,EACvB,SAA6B,EAC7B,MAAmD,EACnD,IAAY,EACZ,KAAc,EACd,SAAqC;;;;;;;wBAErC,IAAI,SAAS,CAAC,OAAO,KAAK,IAAI,IAAI,SAAS,CAAC,QAAQ,KAAK,IAAI;4BACzD,IAAI,UAAU,CAAC,UAAU,KAAK,SAAS;gCACnC,MAAM,IAAI,KAAK,CACX,qFAAqF,CACxF,CAAC;wBAMJ,IAAI,yBACH,CAAC,MAAA,UAAU,CAAC,OAAO,mCAAI,EAAE,CAAC,KAC7B,MAAM,QAAA,EACN,OAAO,EACH,SAAS,CAAC,OAAO,KAAK,KAAK;gCAC3B,KAAK,KAAK,SAAS;gCACnB,OAAO,KAAK,KAAK,QAAQ;gCACrB,CAAC,uBACQ,UAAU,CAAC,OAAO,KACrB,cAAc,EAAE,kBAAkB,IAExC,CAAC,CAAC,MAAA,UAAU,CAAC,OAAO,mCAA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AC,OAAO,CAAC;;oBAClE,sBAAQ,MAAc,CAAC,KAAK,EAAC;oBAEjC,sBAAO,MAAM,CAAC,KAAK,EAAC;;;KACvB,CAAC,CAAC;AAEH,SAAS,iBAAiB,CAAC,OAAgB;IACvC,IAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,OAAO,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG,IAAK,OAAA,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,EAArB,CAAqB,CAAC,CAAC;IACvD,OAAO,MAAM,CAAC;AAClB,CAAC"}
@@ -15,7 +15,7 @@ import { IRandomGenerator } from "./IRandomGenerator";
15
15
  * @author Jenogho Nam - https://github.com/samchon
16
16
  * @author Seungjun We - https://github.com/SeungjunWe
17
17
  */
18
- export interface IConnection {
18
+ export interface IConnection<Headers extends object = {}> {
19
19
  /**
20
20
  * Host address of the remote HTTP server.
21
21
  */
@@ -23,7 +23,7 @@ export interface IConnection {
23
23
  /**
24
24
  * Header values delivered to the remote HTTP server.
25
25
  */
26
- headers?: Record<string, string>;
26
+ headers?: Record<string, IConnection.HeaderValue> & IConnection.Headerify<Headers>;
27
27
  /**
28
28
  * Encryption password of its closure function.
29
29
  */
@@ -107,4 +107,8 @@ export declare namespace IConnection {
107
107
  */
108
108
  referrerPolicy?: "" | "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url";
109
109
  }
110
+ type HeaderValue = string | boolean | number | bigint | string | Array<boolean> | Array<number> | Array<bigint> | Array<number> | Array<string>;
111
+ type Headerify<T extends object> = {
112
+ [P in keyof T]?: T[P] extends HeaderValue ? T[P] | undefined : never;
113
+ };
110
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestia/fetcher",
3
- "version": "1.4.0",
3
+ "version": "1.5.0-dev.20230802",
4
4
  "description": "Fetcher library of Nestia SDK",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
package/src/Fetcher.ts CHANGED
@@ -77,28 +77,39 @@ export class Fetcher {
77
77
  // REQUEST MESSSAGE
78
78
  //----
79
79
  // METHOD & HEADERS
80
+ const headers: Record<string, IConnection.HeaderValue | undefined> = {
81
+ ...(connection.headers ?? {}),
82
+ "Content-Type":
83
+ input !== undefined
84
+ ? connection.headers?.["Content-Type"] ??
85
+ (encrypted.request || typeof input === "string")
86
+ ? "text/plain"
87
+ : "application/json"
88
+ : undefined,
89
+ };
90
+
80
91
  const init: RequestInit = {
81
92
  ...(connection.options ?? {}),
82
93
  method,
83
- headers:
84
- encrypted.request === false &&
85
- input !== undefined &&
86
- typeof input === "object"
87
- ? {
88
- ...connection.headers,
89
- "Content-Type": "application/json",
90
- }
91
- : connection.headers ?? {},
94
+ headers: (() => {
95
+ const output: [string, string][] = [];
96
+ for (const [key, value] of Object.entries(headers))
97
+ if (value === undefined) continue;
98
+ else if (Array.isArray(value))
99
+ for (const v of value) output.push([key, String(v)]);
100
+ else output.push([key, String(value)]);
101
+ return output;
102
+ })(),
92
103
  };
93
- if (encrypted.request || typeof input === "string")
94
- (init.headers as Record<string, string>)["Content-Type"] =
95
- "text/plain";
96
104
 
97
105
  // REQUEST BODY (WITH ENCRYPTION)
98
106
  if (input !== undefined)
99
107
  init.body = (() => {
100
- const json: string = (stringify ?? JSON.stringify)(input);
101
- if (!encrypted.request) return json;
108
+ const json: string =
109
+ headers["Content-Type"] !== "text/plain"
110
+ ? (stringify ?? JSON.stringify)(input)
111
+ : String(input);
112
+ if (encrypted.request !== true) return json;
102
113
 
103
114
  const password:
104
115
  | IEncryptionPassword
@@ -172,7 +183,13 @@ export class Fetcher {
172
183
  content as any;
173
184
  try {
174
185
  // PARSE RESPONSE BODY
175
- ret = JSON.parse(ret as any);
186
+ if (
187
+ encrypted.response ||
188
+ (response.headers.get("Content-Type") ?? "").indexOf(
189
+ "application/json",
190
+ ) !== -1
191
+ )
192
+ ret = JSON.parse(ret as any);
176
193
 
177
194
  // FIND __SET_HEADERS__ FIELD
178
195
  if (
@@ -16,7 +16,7 @@ import { IRandomGenerator } from "./IRandomGenerator";
16
16
  * @author Jenogho Nam - https://github.com/samchon
17
17
  * @author Seungjun We - https://github.com/SeungjunWe
18
18
  */
19
- export interface IConnection {
19
+ export interface IConnection<Headers extends object = {}> {
20
20
  /**
21
21
  * Host address of the remote HTTP server.
22
22
  */
@@ -25,7 +25,8 @@ export interface IConnection {
25
25
  /**
26
26
  * Header values delivered to the remote HTTP server.
27
27
  */
28
- headers?: Record<string, string>;
28
+ headers?: Record<string, IConnection.HeaderValue> &
29
+ IConnection.Headerify<Headers>;
29
30
 
30
31
  /**
31
32
  * Encryption password of its closure function.
@@ -134,4 +135,19 @@ export namespace IConnection {
134
135
  | "strict-origin-when-cross-origin"
135
136
  | "unsafe-url";
136
137
  }
138
+
139
+ export type HeaderValue =
140
+ | string
141
+ | boolean
142
+ | number
143
+ | bigint
144
+ | string
145
+ | Array<boolean>
146
+ | Array<number>
147
+ | Array<bigint>
148
+ | Array<number>
149
+ | Array<string>;
150
+ export type Headerify<T extends object> = {
151
+ [P in keyof T]?: T[P] extends HeaderValue ? T[P] | undefined : never;
152
+ };
137
153
  }