@nymphjs/server 1.0.0-beta.5 → 1.0.0-beta.51

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? = [
@@ -54,36 +64,34 @@ export class EmployeeModel extends EntityServer<EmployeeModelData> {
54
64
  ];
55
65
 
56
66
  static async factory(
57
- guid?: string
67
+ guid?: string,
58
68
  ): Promise<EmployeeModel & EmployeeModelData> {
59
69
  return (await super.factory(guid)) as EmployeeModel & EmployeeModelData;
60
70
  }
61
71
 
62
- static factorySync(guid?: string): EmployeeModel & EmployeeModelData {
63
- return super.factorySync(guid) as EmployeeModel & EmployeeModelData;
72
+ static factorySync(): EmployeeModel & EmployeeModelData {
73
+ return super.factorySync() as EmployeeModel & EmployeeModelData;
64
74
  }
65
75
 
66
- constructor(guid?: string) {
67
- super(guid);
76
+ constructor() {
77
+ super();
68
78
 
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
- }
79
+ this.$addTag('employee');
80
+ this.$data.current = true;
81
+ this.$data.startDate = Date.now();
82
+ this.$data.subordinates = [];
83
+ if (!IS_MANAGER) {
84
+ this.$privateData.push('salary');
77
85
  }
78
86
  }
79
87
 
80
88
  public async $save() {
81
89
  // Validate employee data.
82
90
  const error = new EntityInvalidDataError('Invalid entity data.');
83
- if (this.$data.name == null || this.$data.name == '') {
91
+ if (this.$data.name == null || this.$data.name === '') {
84
92
  error.addField('name');
85
93
  }
86
- if (this.$data.title == null || this.$data.title == '') {
94
+ if (this.$data.title == null || this.$data.title === '') {
87
95
  error.addField('title');
88
96
  }
89
97
  if (this.$data.startDate == null) {
@@ -113,14 +121,45 @@ export class EmployeeModel extends EntityServer<EmployeeModelData> {
113
121
  return value * 2;
114
122
  }
115
123
 
124
+ public static *testStaticIterable(value: number) {
125
+ yield value + 1;
126
+ yield value + 2;
127
+ yield value + 3;
128
+ }
129
+
130
+ public static *testStaticIterableAbort(): Iterator<number, void, boolean> {
131
+ let aborted = yield 1;
132
+
133
+ if (!aborted) {
134
+ throw new Error(
135
+ "testStaticIterableAbort wasn't aborted after the first iteration.",
136
+ );
137
+ }
138
+ }
139
+
116
140
  public static throwErrorStatic() {
117
141
  throw new BadFunctionCallError('This function only throws errors.');
118
142
  }
119
143
 
144
+ public static *throwErrorStaticIterable() {
145
+ yield 1;
146
+ throw new BadFunctionCallError(
147
+ 'This function throws errors after the first iteration.',
148
+ );
149
+ }
150
+
120
151
  public $throwError() {
121
152
  throw new BadFunctionCallError('This function only throws errors.');
122
153
  }
123
154
 
155
+ public $throwHttpError() {
156
+ throw new HttpError('A 501 HTTP error.', 501);
157
+ }
158
+
159
+ public $throwHttpErrorWithDescription() {
160
+ throw new HttpError('A 512 HTTP error.', 512, 'Some Error');
161
+ }
162
+
124
163
  public static inaccessibleMethod() {
125
164
  return true;
126
165
  }
@@ -139,22 +178,20 @@ export class Employee extends Entity<EmployeeData> {
139
178
  // The name of the server class
140
179
  public static class = 'Employee';
141
180
 
142
- constructor(guid?: string) {
143
- super(guid);
181
+ constructor() {
182
+ super();
144
183
 
145
- if (guid == null) {
146
- this.$addTag('employee');
147
- this.$data.current = true;
148
- this.$data.subordinates = [];
149
- }
184
+ this.$addTag('employee');
185
+ this.$data.current = true;
186
+ this.$data.subordinates = [];
150
187
  }
151
188
 
152
189
  static async factory(guid?: string): Promise<Employee & EmployeeData> {
153
190
  return (await super.factory(guid)) as Employee & EmployeeData;
154
191
  }
155
192
 
156
- static factorySync(guid?: string): Employee & EmployeeData {
157
- return super.factorySync(guid) as Employee & EmployeeData;
193
+ static factorySync(): Employee & EmployeeData {
194
+ return super.factorySync() as Employee & EmployeeData;
158
195
  }
159
196
 
160
197
  $testMethod(value: number) {
@@ -169,17 +206,184 @@ export class Employee extends Entity<EmployeeData> {
169
206
  return this.$serverCall('$throwError', []);
170
207
  }
171
208
 
209
+ $throwHttpError() {
210
+ return this.$serverCall('$throwHttpError', []);
211
+ }
212
+
213
+ $throwHttpErrorWithDescription() {
214
+ return this.$serverCall('$throwHttpErrorWithDescription', []);
215
+ }
216
+
172
217
  static testStatic(value: number) {
173
- return Employee.serverCallStatic('testStatic', [value]);
218
+ return this.serverCallStatic('testStatic', [value]);
219
+ }
220
+
221
+ static testStaticIterable(value: number) {
222
+ return this.serverCallStaticIterator('testStaticIterable', [value]);
223
+ }
224
+
225
+ static testStaticIterableAbort() {
226
+ return this.serverCallStaticIterator('testStaticIterableAbort', []);
174
227
  }
175
228
 
176
229
  static throwErrorStatic() {
177
- return Employee.serverCallStatic('throwErrorStatic', []);
230
+ return this.serverCallStatic('throwErrorStatic', []);
231
+ }
232
+
233
+ static throwErrorStaticIterable() {
234
+ return this.serverCallStaticIterator('throwErrorStaticIterable', []);
178
235
  }
179
236
 
180
237
  static inaccessibleMethod() {
181
- return Employee.serverCallStatic('inaccessibleMethod', []);
238
+ return this.serverCallStatic('inaccessibleMethod', []);
182
239
  }
183
240
  }
184
241
 
185
- export default Employee;
242
+ export type RestrictedModelData = {
243
+ name: string;
244
+ };
245
+
246
+ /**
247
+ * This class is a test class that extends the Entity class.
248
+ */
249
+ export class RestrictedModel extends EntityServer<RestrictedModelData> {
250
+ static ETYPE = 'restricted';
251
+ static class = 'Restricted';
252
+
253
+ public static restEnabled = false;
254
+
255
+ static async factory(
256
+ guid?: string,
257
+ ): Promise<RestrictedModel & RestrictedModelData> {
258
+ return (await super.factory(guid)) as RestrictedModel & RestrictedModelData;
259
+ }
260
+
261
+ static factorySync(): RestrictedModel & RestrictedModelData {
262
+ return super.factorySync() as RestrictedModel & RestrictedModelData;
263
+ }
264
+
265
+ constructor() {
266
+ super();
267
+
268
+ this.$data.name = '';
269
+ }
270
+
271
+ public async $save() {
272
+ // Validate entity data.
273
+ const error = new EntityInvalidDataError('Invalid entity data.');
274
+ if (this.$data.name == null || this.$data.name === '') {
275
+ error.addField('name');
276
+ }
277
+ if (error.getFields().length) {
278
+ throw error;
279
+ }
280
+ return await super.$save();
281
+ }
282
+
283
+ $testMethod(value: string) {
284
+ return value;
285
+ }
286
+
287
+ public static testStatic(value: number) {
288
+ return value;
289
+ }
290
+ }
291
+
292
+ export type RestrictedData = {
293
+ name: string;
294
+ };
295
+
296
+ export class Restricted extends Entity<RestrictedData> {
297
+ // The name of the server class
298
+ public static class = 'Restricted';
299
+
300
+ constructor() {
301
+ super();
302
+
303
+ this.$data.name = '';
304
+ }
305
+
306
+ static async factory(guid?: string): Promise<Restricted & RestrictedData> {
307
+ return (await super.factory(guid)) as Restricted & RestrictedData;
308
+ }
309
+
310
+ static factorySync(): Restricted & RestrictedData {
311
+ return super.factorySync() as Restricted & RestrictedData;
312
+ }
313
+
314
+ $testMethod(value: number) {
315
+ return this.$serverCall('$testMethod', [value]);
316
+ }
317
+
318
+ static testStatic(value: number) {
319
+ return this.serverCallStatic('testStatic', [value]);
320
+ }
321
+ }
322
+
323
+ export type PubSubDisabledModelData = {
324
+ name: string;
325
+ };
326
+
327
+ /**
328
+ * This class is a test class that extends the Entity class.
329
+ */
330
+ export class PubSubDisabledModel extends EntityServer<PubSubDisabledModelData> {
331
+ static ETYPE = 'pubsub_disabled';
332
+ static class = 'PubSubDisabled';
333
+
334
+ public static pubSubEnabled = false;
335
+
336
+ static async factory(
337
+ guid?: string,
338
+ ): Promise<PubSubDisabledModel & PubSubDisabledModelData> {
339
+ return (await super.factory(guid)) as PubSubDisabledModel &
340
+ PubSubDisabledModelData;
341
+ }
342
+
343
+ static factorySync(): PubSubDisabledModel & PubSubDisabledModelData {
344
+ return super.factorySync() as PubSubDisabledModel & PubSubDisabledModelData;
345
+ }
346
+
347
+ constructor() {
348
+ super();
349
+
350
+ this.$data.name = '';
351
+ }
352
+
353
+ public async $save() {
354
+ // Validate entity data.
355
+ const error = new EntityInvalidDataError('Invalid entity data.');
356
+ if (this.$data.name == null || this.$data.name === '') {
357
+ error.addField('name');
358
+ }
359
+ if (error.getFields().length) {
360
+ throw error;
361
+ }
362
+ return await super.$save();
363
+ }
364
+ }
365
+
366
+ export type PubSubDisabledData = {
367
+ name: string;
368
+ };
369
+
370
+ export class PubSubDisabled extends Entity<PubSubDisabledData> {
371
+ // The name of the server class
372
+ public static class = 'PubSubDisabled';
373
+
374
+ constructor() {
375
+ super();
376
+
377
+ this.$data.name = '';
378
+ }
379
+
380
+ static async factory(
381
+ guid?: string,
382
+ ): Promise<PubSubDisabled & PubSubDisabledData> {
383
+ return (await super.factory(guid)) as PubSubDisabled & PubSubDisabledData;
384
+ }
385
+
386
+ static factorySync(): PubSubDisabled & PubSubDisabledData {
387
+ return super.factorySync() as PubSubDisabled & PubSubDisabledData;
388
+ }
389
+ }
package/typedoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../typedoc.base.json"],
3
+ "entryPoints": ["src/index.ts"]
4
+ }