@nymphjs/server 1.0.0-beta.8 → 1.0.0-beta.80

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.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * HTTP status code to status text map.
3
+ */
4
+ export const statusDescriptions: { [k: number]: string } = {
5
+ 100: 'Continue',
6
+ 101: 'Switching Protocols',
7
+ 102: 'Processing',
8
+ 103: 'Early Hints',
9
+ 200: 'OK',
10
+ 201: 'Created',
11
+ 202: 'Accepted',
12
+ 203: 'Non-Authoritative Information',
13
+ 204: 'No Content',
14
+ 205: 'Reset Content',
15
+ 206: 'Partial Content',
16
+ 207: 'Multi-Status',
17
+ 208: 'Already Reported',
18
+ 226: 'IM Used',
19
+ 300: 'Multiple Choices',
20
+ 301: 'Moved Permanently',
21
+ 302: 'Found',
22
+ 303: 'See Other',
23
+ 304: 'Not Modified',
24
+ 305: 'Use Proxy',
25
+ 306: 'Switch Proxy',
26
+ 307: 'Temporary Redirect',
27
+ 308: 'Permanent Redirect',
28
+ 400: 'Bad Request',
29
+ 401: 'Unauthorized',
30
+ 402: 'Payment Required',
31
+ 403: 'Forbidden',
32
+ 404: 'Not Found',
33
+ 405: 'Method Not Allowed',
34
+ 406: 'Not Acceptable',
35
+ 407: 'Proxy Authentication Required',
36
+ 408: 'Request Timeout',
37
+ 409: 'Conflict',
38
+ 410: 'Gone',
39
+ 411: 'Length Required',
40
+ 412: 'Precondition Failed',
41
+ 413: 'Payload Too Large',
42
+ 414: 'URI Too Long',
43
+ 415: 'Unsupported Media Type',
44
+ 416: 'Range Not Satisfiable',
45
+ 417: 'Expectation Failed',
46
+ 418: "I'm a teapot",
47
+ 421: 'Misdirected Request',
48
+ 422: 'Unprocessable Entity',
49
+ 423: 'Locked',
50
+ 424: 'Failed Dependency',
51
+ 425: 'Too Early',
52
+ 426: 'Upgrade Required',
53
+ 428: 'Precondition Required',
54
+ 429: 'Too Many Requests',
55
+ 431: 'Request Header Fields Too Large',
56
+ 451: 'Unavailable For Legal Reasons',
57
+ 500: 'Internal Server Error',
58
+ 501: 'Not Implemented',
59
+ 502: 'Bad Gateway',
60
+ 503: 'Service Unavailable',
61
+ 504: 'Gateway Timeout',
62
+ 505: 'HTTP Version Not Supported',
63
+ 506: 'Variant Also Negotiates',
64
+ 507: 'Insufficient Storage',
65
+ 508: 'Loop Detected',
66
+ 510: 'Not Extended',
67
+ 511: 'Network Authentication Required',
68
+ };
@@ -1,6 +1,8 @@
1
1
  import { Entity as EntityServer, EntityInvalidDataError } from '@nymphjs/nymph';
2
2
  import { Entity } from '@nymphjs/client';
3
3
 
4
+ import { HttpError } from './HttpError';
5
+
4
6
  export type EmployeeBaseData<T> = {
5
7
  name?: string;
6
8
  id?: number;
@@ -34,8 +36,16 @@ export class EmployeeModel extends EntityServer<EmployeeModelData> {
34
36
  '$testMethodStateless',
35
37
  '$testMethod',
36
38
  '$throwError',
39
+ '$throwHttpError',
40
+ '$throwHttpErrorWithDescription',
41
+ ];
42
+ public static clientEnabledStaticMethods = [
43
+ 'testStatic',
44
+ 'testStaticIterable',
45
+ 'testStaticIterableAbort',
46
+ 'throwErrorStatic',
47
+ 'throwErrorStaticIterable',
37
48
  ];
38
- public static clientEnabledStaticMethods = ['testStatic', 'throwErrorStatic'];
39
49
  protected $protectedTags = ['employee'];
40
50
  protected $allowlistTags? = ['boss', 'bigcheese'];
41
51
  protected $allowlistData? = [
@@ -53,37 +63,25 @@ export class EmployeeModel extends EntityServer<EmployeeModelData> {
53
63
  'building',
54
64
  ];
55
65
 
56
- static async factory(
57
- guid?: string
58
- ): Promise<EmployeeModel & EmployeeModelData> {
59
- return (await super.factory(guid)) as EmployeeModel & EmployeeModelData;
60
- }
66
+ constructor() {
67
+ super();
61
68
 
62
- static factorySync(guid?: string): EmployeeModel & EmployeeModelData {
63
- return super.factorySync(guid) as EmployeeModel & EmployeeModelData;
64
- }
65
-
66
- constructor(guid?: string) {
67
- super(guid);
68
-
69
- if (this.guid == null) {
70
- this.$addTag('employee');
71
- this.$data.current = true;
72
- this.$data.startDate = Date.now();
73
- this.$data.subordinates = [];
74
- if (!IS_MANAGER) {
75
- this.$privateData.push('salary');
76
- }
69
+ this.$addTag('employee');
70
+ this.$data.current = true;
71
+ this.$data.startDate = Date.now();
72
+ this.$data.subordinates = [];
73
+ if (!IS_MANAGER) {
74
+ this.$privateData.push('salary');
77
75
  }
78
76
  }
79
77
 
80
78
  public async $save() {
81
79
  // Validate employee data.
82
80
  const error = new EntityInvalidDataError('Invalid entity data.');
83
- if (this.$data.name == null || this.$data.name == '') {
81
+ if (this.$data.name == null || this.$data.name === '') {
84
82
  error.addField('name');
85
83
  }
86
- if (this.$data.title == null || this.$data.title == '') {
84
+ if (this.$data.title == null || this.$data.title === '') {
87
85
  error.addField('title');
88
86
  }
89
87
  if (this.$data.startDate == null) {
@@ -113,14 +111,45 @@ export class EmployeeModel extends EntityServer<EmployeeModelData> {
113
111
  return value * 2;
114
112
  }
115
113
 
114
+ public static *testStaticIterable(value: number) {
115
+ yield value + 1;
116
+ yield value + 2;
117
+ yield value + 3;
118
+ }
119
+
120
+ public static *testStaticIterableAbort(): Iterator<number, void, boolean> {
121
+ let aborted = yield 1;
122
+
123
+ if (!aborted) {
124
+ throw new Error(
125
+ "testStaticIterableAbort wasn't aborted after the first iteration.",
126
+ );
127
+ }
128
+ }
129
+
116
130
  public static throwErrorStatic() {
117
131
  throw new BadFunctionCallError('This function only throws errors.');
118
132
  }
119
133
 
134
+ public static *throwErrorStaticIterable() {
135
+ yield 1;
136
+ throw new BadFunctionCallError(
137
+ 'This function throws errors after the first iteration.',
138
+ );
139
+ }
140
+
120
141
  public $throwError() {
121
142
  throw new BadFunctionCallError('This function only throws errors.');
122
143
  }
123
144
 
145
+ public $throwHttpError() {
146
+ throw new HttpError('A 501 HTTP error.', 501);
147
+ }
148
+
149
+ public $throwHttpErrorWithDescription() {
150
+ throw new HttpError('A 512 HTTP error.', 512, 'Some Error');
151
+ }
152
+
124
153
  public static inaccessibleMethod() {
125
154
  return true;
126
155
  }
@@ -139,22 +168,12 @@ export class Employee extends Entity<EmployeeData> {
139
168
  // The name of the server class
140
169
  public static class = 'Employee';
141
170
 
142
- constructor(guid?: string) {
143
- super(guid);
171
+ constructor() {
172
+ super();
144
173
 
145
- if (guid == null) {
146
- this.$addTag('employee');
147
- this.$data.current = true;
148
- this.$data.subordinates = [];
149
- }
150
- }
151
-
152
- static async factory(guid?: string): Promise<Employee & EmployeeData> {
153
- return (await super.factory(guid)) as Employee & EmployeeData;
154
- }
155
-
156
- static factorySync(guid?: string): Employee & EmployeeData {
157
- return super.factorySync(guid) as Employee & EmployeeData;
174
+ this.$addTag('employee');
175
+ this.$data.current = true;
176
+ this.$data.subordinates = [];
158
177
  }
159
178
 
160
179
  $testMethod(value: number) {
@@ -169,17 +188,145 @@ export class Employee extends Entity<EmployeeData> {
169
188
  return this.$serverCall('$throwError', []);
170
189
  }
171
190
 
191
+ $throwHttpError() {
192
+ return this.$serverCall('$throwHttpError', []);
193
+ }
194
+
195
+ $throwHttpErrorWithDescription() {
196
+ return this.$serverCall('$throwHttpErrorWithDescription', []);
197
+ }
198
+
172
199
  static testStatic(value: number) {
173
200
  return this.serverCallStatic('testStatic', [value]);
174
201
  }
175
202
 
203
+ static testStaticIterable(value: number) {
204
+ return this.serverCallStaticIterator('testStaticIterable', [value]);
205
+ }
206
+
207
+ static testStaticIterableAbort() {
208
+ return this.serverCallStaticIterator('testStaticIterableAbort', []);
209
+ }
210
+
176
211
  static throwErrorStatic() {
177
212
  return this.serverCallStatic('throwErrorStatic', []);
178
213
  }
179
214
 
215
+ static throwErrorStaticIterable() {
216
+ return this.serverCallStaticIterator('throwErrorStaticIterable', []);
217
+ }
218
+
180
219
  static inaccessibleMethod() {
181
220
  return this.serverCallStatic('inaccessibleMethod', []);
182
221
  }
183
222
  }
184
223
 
185
- export default Employee;
224
+ export type RestrictedModelData = {
225
+ name: string;
226
+ };
227
+
228
+ /**
229
+ * This class is a test class that extends the Entity class.
230
+ */
231
+ export class RestrictedModel extends EntityServer<RestrictedModelData> {
232
+ static ETYPE = 'restricted';
233
+ static class = 'Restricted';
234
+
235
+ public static restEnabled = false;
236
+
237
+ constructor() {
238
+ super();
239
+
240
+ this.$data.name = '';
241
+ }
242
+
243
+ public async $save() {
244
+ // Validate entity data.
245
+ const error = new EntityInvalidDataError('Invalid entity data.');
246
+ if (this.$data.name == null || this.$data.name === '') {
247
+ error.addField('name');
248
+ }
249
+ if (error.getFields().length) {
250
+ throw error;
251
+ }
252
+ return await super.$save();
253
+ }
254
+
255
+ $testMethod(value: string) {
256
+ return value;
257
+ }
258
+
259
+ public static testStatic(value: number) {
260
+ return value;
261
+ }
262
+ }
263
+
264
+ export type RestrictedData = {
265
+ name: string;
266
+ };
267
+
268
+ export class Restricted extends Entity<RestrictedData> {
269
+ // The name of the server class
270
+ public static class = 'Restricted';
271
+
272
+ constructor() {
273
+ super();
274
+
275
+ this.$data.name = '';
276
+ }
277
+
278
+ $testMethod(value: number) {
279
+ return this.$serverCall('$testMethod', [value]);
280
+ }
281
+
282
+ static testStatic(value: number) {
283
+ return this.serverCallStatic('testStatic', [value]);
284
+ }
285
+ }
286
+
287
+ export type PubSubDisabledModelData = {
288
+ name: string;
289
+ };
290
+
291
+ /**
292
+ * This class is a test class that extends the Entity class.
293
+ */
294
+ export class PubSubDisabledModel extends EntityServer<PubSubDisabledModelData> {
295
+ static ETYPE = 'pubsub_disabled';
296
+ static class = 'PubSubDisabled';
297
+
298
+ public static pubSubEnabled = false;
299
+
300
+ constructor() {
301
+ super();
302
+
303
+ this.$data.name = '';
304
+ }
305
+
306
+ public async $save() {
307
+ // Validate entity data.
308
+ const error = new EntityInvalidDataError('Invalid entity data.');
309
+ if (this.$data.name == null || this.$data.name === '') {
310
+ error.addField('name');
311
+ }
312
+ if (error.getFields().length) {
313
+ throw error;
314
+ }
315
+ return await super.$save();
316
+ }
317
+ }
318
+
319
+ export type PubSubDisabledData = {
320
+ name: string;
321
+ };
322
+
323
+ export class PubSubDisabled extends Entity<PubSubDisabledData> {
324
+ // The name of the server class
325
+ public static class = 'PubSubDisabled';
326
+
327
+ constructor() {
328
+ super();
329
+
330
+ this.$data.name = '';
331
+ }
332
+ }
package/tsconfig.json CHANGED
@@ -2,10 +2,10 @@
2
2
  "extends": "@tsconfig/recommended/tsconfig.json",
3
3
 
4
4
  "compilerOptions": {
5
- "lib": ["ES2021"],
6
- "target": "ES2021",
5
+ "lib": ["ES2023"],
6
+ "target": "ES2022",
7
7
  "noImplicitAny": true,
8
- "removeComments": true,
8
+ "removeComments": false,
9
9
  "sourceMap": true,
10
10
  "outDir": "dist",
11
11
  "resolveJsonModule": true,
package/typedoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../typedoc.base.json"],
3
+ "entryPoints": ["src/index.ts"]
4
+ }