UNPKG

@nymphjs/server

Version:

Nymph.js - REST Server

237 lines 6.92 kB
import { Entity as EntityServer, EntityInvalidDataError } from '@nymphjs/nymph'; import { Entity } from '@nymphjs/client'; import { HttpError } from './HttpError.js'; const IS_MANAGER = true; /** * This class is a test class that extends the Entity class. */ export class EmployeeModel extends EntityServer { static ETYPE = 'employee'; static class = 'Employee'; $clientEnabledMethods = [ '$testMethodStateless', '$testMethod', '$throwError', '$throwHttpError', '$throwHttpErrorWithDescription', ]; static clientEnabledStaticMethods = [ 'testStatic', 'testStaticIterable', 'testStaticIterableAbort', 'throwErrorStatic', 'throwErrorStaticIterable', ]; $protectedTags = ['employee']; $allowlistTags = ['boss', 'bigcheese']; $allowlistData = [ 'name', 'id', 'title', 'department', 'subordinates', 'salary', 'current', 'startDate', 'endDate', 'phone', 'manager', 'building', ]; constructor() { super(); this.$addTag('employee'); this.$data.current = true; this.$data.startDate = Date.now(); this.$data.subordinates = []; if (!IS_MANAGER) { this.$privateData.push('salary'); } } async $save() { // Validate employee data. const error = new EntityInvalidDataError('Invalid entity data.'); if (this.$data.name == null || this.$data.name === '') { error.addField('name'); } if (this.$data.title == null || this.$data.title === '') { error.addField('title'); } if (this.$data.startDate == null) { error.addField('startDate'); } if (error.getFields().length) { throw error; } // Generate employee ID. if (this.$data.id == null) { this.$data.id = (await this.$nymph.newUID('employee')) ?? undefined; } return await super.$save(); } $testMethodStateless(value) { this.$data.name = 'bad name'; return value + 1; } $testMethod(value) { this.$data.current = false; return value + 2; } static testStatic(value) { return value * 2; } static *testStaticIterable(value) { yield value + 1; yield value + 2; yield value + 3; } static *testStaticIterableAbort() { let aborted = yield 1; if (!aborted) { throw new Error("testStaticIterableAbort wasn't aborted after the first iteration."); } } static throwErrorStatic() { throw new BadFunctionCallError('This function only throws errors.'); } static *throwErrorStaticIterable() { yield 1; throw new BadFunctionCallError('This function throws errors after the first iteration.'); } $throwError() { throw new BadFunctionCallError('This function only throws errors.'); } $throwHttpError() { throw new HttpError('A 501 HTTP error.', 501); } $throwHttpErrorWithDescription() { throw new HttpError('A 512 HTTP error.', 512, 'Some Error'); } static inaccessibleMethod() { return true; } } export class BadFunctionCallError extends Error { constructor(message) { super(message); this.name = 'BadFunctionCallError'; } } export class Employee extends Entity { // The name of the server class static class = 'Employee'; constructor() { super(); this.$addTag('employee'); this.$data.current = true; this.$data.subordinates = []; } $testMethod(value) { return this.$serverCall('$testMethod', [value]); } $testMethodStateless(value) { return this.$serverCall('$testMethodStateless', [value], true); } $throwError() { return this.$serverCall('$throwError', []); } $throwHttpError() { return this.$serverCall('$throwHttpError', []); } $throwHttpErrorWithDescription() { return this.$serverCall('$throwHttpErrorWithDescription', []); } static testStatic(value) { return this.serverCallStatic('testStatic', [value]); } static testStaticIterable(value) { return this.serverCallStaticIterator('testStaticIterable', [value]); } static testStaticIterableAbort() { return this.serverCallStaticIterator('testStaticIterableAbort', []); } static throwErrorStatic() { return this.serverCallStatic('throwErrorStatic', []); } static throwErrorStaticIterable() { return this.serverCallStaticIterator('throwErrorStaticIterable', []); } static inaccessibleMethod() { return this.serverCallStatic('inaccessibleMethod', []); } } /** * This class is a test class that extends the Entity class. */ export class RestrictedModel extends EntityServer { static ETYPE = 'restricted'; static class = 'Restricted'; static restEnabled = false; constructor() { super(); this.$data.name = ''; } async $save() { // Validate entity data. const error = new EntityInvalidDataError('Invalid entity data.'); if (this.$data.name == null || this.$data.name === '') { error.addField('name'); } if (error.getFields().length) { throw error; } return await super.$save(); } $testMethod(value) { return value; } static testStatic(value) { return value; } } export class Restricted extends Entity { // The name of the server class static class = 'Restricted'; constructor() { super(); this.$data.name = ''; } $testMethod(value) { return this.$serverCall('$testMethod', [value]); } static testStatic(value) { return this.serverCallStatic('testStatic', [value]); } } /** * This class is a test class that extends the Entity class. */ export class PubSubDisabledModel extends EntityServer { static ETYPE = 'pubsub_disabled'; static class = 'PubSubDisabled'; static pubSubEnabled = false; constructor() { super(); this.$data.name = ''; } async $save() { // Validate entity data. const error = new EntityInvalidDataError('Invalid entity data.'); if (this.$data.name == null || this.$data.name === '') { error.addField('name'); } if (error.getFields().length) { throw error; } return await super.$save(); } } export class PubSubDisabled extends Entity { // The name of the server class static class = 'PubSubDisabled'; constructor() { super(); this.$data.name = ''; } } //# sourceMappingURL=testArtifacts.js.map