@opra/core 0.0.12 → 0.1.0
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/cjs/enums/http-headers.enum.js +2 -1
- package/cjs/exception/http-errors/not-acceptable.error.js +26 -0
- package/cjs/exception/index.js +2 -0
- package/cjs/exception/resource-errors/resource-not-found.error.js +19 -0
- package/cjs/implementation/adapter-utils/entity-resource-execute.util.js +84 -0
- package/cjs/implementation/adapter-utils/resource-execute.util.js +11 -0
- package/cjs/implementation/adapter-utils/resource-prepare.util.js +11 -0
- package/cjs/implementation/{adapter/adapter.js → adapter.js} +40 -7
- package/cjs/implementation/{adapter/express-adapter.js → express-adapter.js} +0 -0
- package/cjs/implementation/http-adapter.js +267 -0
- package/cjs/implementation/query-context.js +5 -3
- package/cjs/index.js +5 -15
- package/cjs/{services/entity-resource-controller.js → interfaces/entity-service.interface.js} +0 -0
- package/cjs/interfaces/query.interface.js +26 -9
- package/cjs/services/json-data-service.js +241 -4
- package/cjs/utils/create-i18n.js +1 -1
- package/cjs/utils/get-caller-file.util.js +6 -1
- package/cjs/utils/{string-path-to-object-tree.js → path-to-tree.js} +3 -3
- package/esm/enums/http-headers.enum.d.ts +2 -1
- package/esm/enums/http-headers.enum.js +2 -1
- package/esm/exception/http-errors/not-acceptable.error.d.ts +10 -0
- package/esm/exception/http-errors/not-acceptable.error.js +22 -0
- package/esm/exception/index.d.ts +2 -0
- package/esm/exception/index.js +2 -0
- package/esm/exception/resource-errors/resource-not-found.error.d.ts +4 -0
- package/esm/exception/resource-errors/resource-not-found.error.js +15 -0
- package/esm/implementation/adapter-utils/entity-resource-execute.util.d.ts +3 -0
- package/esm/implementation/adapter-utils/entity-resource-execute.util.js +80 -0
- package/esm/implementation/adapter-utils/resource-execute.util.d.ts +3 -0
- package/esm/implementation/adapter-utils/resource-execute.util.js +7 -0
- package/esm/implementation/adapter-utils/resource-prepare.util.d.ts +3 -0
- package/esm/implementation/adapter-utils/resource-prepare.util.js +7 -0
- package/esm/implementation/{adapter/adapter.d.ts → adapter.d.ts} +5 -9
- package/esm/implementation/{adapter/adapter.js → adapter.js} +39 -6
- package/esm/implementation/{adapter/express-adapter.d.ts → express-adapter.d.ts} +2 -2
- package/esm/implementation/{adapter/express-adapter.js → express-adapter.js} +0 -0
- package/esm/implementation/{adapter/http-adapter.d.ts → http-adapter.d.ts} +6 -6
- package/esm/implementation/http-adapter.js +263 -0
- package/esm/implementation/query-context.d.ts +3 -4
- package/esm/implementation/query-context.js +5 -3
- package/esm/index.d.ts +5 -15
- package/esm/index.js +5 -15
- package/esm/{services/entity-resource-controller.d.ts → interfaces/entity-service.interface.d.ts} +0 -0
- package/esm/{services/entity-resource-controller.js → interfaces/entity-service.interface.js} +0 -0
- package/esm/interfaces/query.interface.d.ts +31 -25
- package/esm/interfaces/query.interface.js +26 -9
- package/esm/services/json-data-service.d.ts +66 -7
- package/esm/services/json-data-service.js +241 -4
- package/esm/types.d.ts +10 -8
- package/esm/utils/create-i18n.d.ts +1 -1
- package/esm/utils/create-i18n.js +1 -1
- package/esm/utils/get-caller-file.util.d.ts +1 -1
- package/esm/utils/get-caller-file.util.js +6 -1
- package/esm/utils/path-to-tree.d.ts +4 -0
- package/esm/utils/{string-path-to-object-tree.js → path-to-tree.js} +1 -1
- package/i18n/en/error.json +3 -0
- package/package.json +6 -7
- package/cjs/constants.js +0 -5
- package/cjs/decorators/entity-resource.decorator.js +0 -24
- package/cjs/implementation/adapter/http-adapter.js +0 -238
- package/cjs/implementation/data-type/complex-type.js +0 -39
- package/cjs/implementation/data-type/data-type.js +0 -35
- package/cjs/implementation/data-type/entity-type.js +0 -33
- package/cjs/implementation/data-type/simple-type.js +0 -30
- package/cjs/implementation/opra-document.js +0 -116
- package/cjs/implementation/opra-service.js +0 -59
- package/cjs/implementation/resource/container-resource-handler.js +0 -30
- package/cjs/implementation/resource/entity-resource-handler.js +0 -80
- package/cjs/implementation/resource/resource-handler.js +0 -31
- package/cjs/implementation/schema-generator.js +0 -163
- package/cjs/interfaces/opra-schema.metadata.js +0 -2
- package/cjs/interfaces/resource-container.interface.js +0 -2
- package/cjs/utils/class-utils.js +0 -37
- package/cjs/utils/headers.js +0 -58
- package/cjs/utils/internal-data-types.js +0 -81
- package/cjs/utils/responsive-object.js +0 -49
- package/cjs/utils/terminal-utils.js +0 -7
- package/esm/constants.d.ts +0 -2
- package/esm/constants.js +0 -2
- package/esm/decorators/entity-resource.decorator.d.ts +0 -5
- package/esm/decorators/entity-resource.decorator.js +0 -19
- package/esm/implementation/adapter/http-adapter.js +0 -234
- package/esm/implementation/data-type/complex-type.d.ts +0 -18
- package/esm/implementation/data-type/complex-type.js +0 -35
- package/esm/implementation/data-type/data-type.d.ts +0 -15
- package/esm/implementation/data-type/data-type.js +0 -31
- package/esm/implementation/data-type/entity-type.d.ts +0 -10
- package/esm/implementation/data-type/entity-type.js +0 -29
- package/esm/implementation/data-type/simple-type.d.ts +0 -15
- package/esm/implementation/data-type/simple-type.js +0 -26
- package/esm/implementation/opra-document.d.ts +0 -26
- package/esm/implementation/opra-document.js +0 -111
- package/esm/implementation/opra-service.d.ts +0 -19
- package/esm/implementation/opra-service.js +0 -55
- package/esm/implementation/resource/container-resource-handler.d.ts +0 -14
- package/esm/implementation/resource/container-resource-handler.js +0 -26
- package/esm/implementation/resource/entity-resource-handler.d.ts +0 -18
- package/esm/implementation/resource/entity-resource-handler.js +0 -75
- package/esm/implementation/resource/resource-handler.d.ts +0 -15
- package/esm/implementation/resource/resource-handler.js +0 -27
- package/esm/implementation/schema-generator.d.ts +0 -21
- package/esm/implementation/schema-generator.js +0 -159
- package/esm/interfaces/opra-schema.metadata.d.ts +0 -14
- package/esm/interfaces/opra-schema.metadata.js +0 -1
- package/esm/interfaces/resource-container.interface.d.ts +0 -6
- package/esm/interfaces/resource-container.interface.js +0 -1
- package/esm/utils/class-utils.d.ts +0 -6
- package/esm/utils/class-utils.js +0 -30
- package/esm/utils/headers.d.ts +0 -9
- package/esm/utils/headers.js +0 -55
- package/esm/utils/internal-data-types.d.ts +0 -5
- package/esm/utils/internal-data-types.js +0 -78
- package/esm/utils/responsive-object.d.ts +0 -3
- package/esm/utils/responsive-object.js +0 -45
- package/esm/utils/string-path-to-object-tree.d.ts +0 -4
- package/esm/utils/terminal-utils.d.ts +0 -4
- package/esm/utils/terminal-utils.js +0 -4
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { EntityResourceHandler } from './entity-resource-handler.js';
|
|
2
|
-
import { ResourceHandler } from './resource-handler.js';
|
|
3
|
-
export class ContainerResourceHandler extends ResourceHandler {
|
|
4
|
-
constructor(args) {
|
|
5
|
-
super({
|
|
6
|
-
kind: 'ContainerResource',
|
|
7
|
-
...args
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
getResource(name) {
|
|
11
|
-
const t = this._args.resources[name];
|
|
12
|
-
if (!t)
|
|
13
|
-
throw new Error(`Resource "${name}" does not exists`);
|
|
14
|
-
return t;
|
|
15
|
-
}
|
|
16
|
-
getEntityResource(name) {
|
|
17
|
-
const t = this.getResource(name);
|
|
18
|
-
if (!(t instanceof EntityResourceHandler))
|
|
19
|
-
throw new Error(`"${name}" is not an EntityResource`);
|
|
20
|
-
return t;
|
|
21
|
-
}
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
-
execute(ctx) {
|
|
24
|
-
return Promise.resolve(undefined);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { StrictOmit } from 'ts-gems';
|
|
2
|
-
import { OpraSchema } from '@opra/schema';
|
|
3
|
-
import { EntityType } from '../data-type/entity-type.js';
|
|
4
|
-
import { OpraService } from '../opra-service.js';
|
|
5
|
-
import { QueryContext } from '../query-context.js';
|
|
6
|
-
import { ResourceHandler } from './resource-handler.js';
|
|
7
|
-
export declare type EntityResourceControllerArgs = StrictOmit<OpraSchema.EntityResource, 'kind'> & {
|
|
8
|
-
service: OpraService;
|
|
9
|
-
dataType: EntityType;
|
|
10
|
-
};
|
|
11
|
-
export declare class EntityResourceHandler extends ResourceHandler {
|
|
12
|
-
protected readonly _args: OpraSchema.EntityResource;
|
|
13
|
-
readonly service: OpraService;
|
|
14
|
-
readonly dataType: EntityType;
|
|
15
|
-
constructor(args: EntityResourceControllerArgs);
|
|
16
|
-
execute(ctx: QueryContext): Promise<void>;
|
|
17
|
-
_executeFn(ctx: QueryContext, fnName: string): Promise<any>;
|
|
18
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import { OpraQuery } from '../../interfaces/query.interface.js';
|
|
3
|
-
import { EntityType } from '../data-type/entity-type.js';
|
|
4
|
-
import { ResourceHandler } from './resource-handler.js';
|
|
5
|
-
var isSearchQuery = OpraQuery.isSearchQuery;
|
|
6
|
-
import { UnprocessableEntityError } from '../../exception/index.js';
|
|
7
|
-
export class EntityResourceHandler extends ResourceHandler {
|
|
8
|
-
service;
|
|
9
|
-
dataType;
|
|
10
|
-
constructor(args) {
|
|
11
|
-
super({
|
|
12
|
-
kind: 'EntityResource',
|
|
13
|
-
..._.omit(args, ['dataType', 'service'])
|
|
14
|
-
});
|
|
15
|
-
this.dataType = args.dataType;
|
|
16
|
-
this.service = args.service;
|
|
17
|
-
// noinspection SuspiciousTypeOfGuard
|
|
18
|
-
if (!(args.dataType instanceof EntityType))
|
|
19
|
-
throw new TypeError(`You should provide an EntityType for EntityResourceController`);
|
|
20
|
-
}
|
|
21
|
-
async execute(ctx) {
|
|
22
|
-
const { query } = ctx;
|
|
23
|
-
if (isSearchQuery(query)) {
|
|
24
|
-
const promises = [];
|
|
25
|
-
let search;
|
|
26
|
-
let count;
|
|
27
|
-
promises.push(this._executeFn(ctx, query.queryType)
|
|
28
|
-
.then(v => search = v));
|
|
29
|
-
if (query.count) {
|
|
30
|
-
promises.push(this._executeFn(ctx, 'count')
|
|
31
|
-
.then(v => count = v));
|
|
32
|
-
}
|
|
33
|
-
await Promise.all(promises);
|
|
34
|
-
ctx.response.value = {
|
|
35
|
-
...search,
|
|
36
|
-
...count
|
|
37
|
-
};
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
ctx.response.value = await this._executeFn(ctx, query.queryType);
|
|
41
|
-
}
|
|
42
|
-
async _executeFn(ctx, fnName) {
|
|
43
|
-
const fn = this._args[fnName];
|
|
44
|
-
let result = typeof fn === 'function' ? (await fn(ctx)) : undefined;
|
|
45
|
-
switch (fnName) {
|
|
46
|
-
case 'search':
|
|
47
|
-
return { items: Array.isArray(result) ? result : (ctx.response.value ? [result] : []) };
|
|
48
|
-
case 'count':
|
|
49
|
-
return { count: result || 0 };
|
|
50
|
-
case 'delete':
|
|
51
|
-
case 'deleteMany':
|
|
52
|
-
case 'updateMany':
|
|
53
|
-
let affectedRecords;
|
|
54
|
-
if (typeof result === 'number')
|
|
55
|
-
affectedRecords = result;
|
|
56
|
-
if (typeof result === 'boolean')
|
|
57
|
-
affectedRecords = result ? 1 : 0;
|
|
58
|
-
if (typeof result === 'object')
|
|
59
|
-
affectedRecords = result.affectedRows || result.affectedRecords;
|
|
60
|
-
return { affectedRecords };
|
|
61
|
-
}
|
|
62
|
-
result = Array.isArray(result) ? result[0] : result;
|
|
63
|
-
if (!result)
|
|
64
|
-
throw new UnprocessableEntityError();
|
|
65
|
-
if (ctx.resultPath) {
|
|
66
|
-
const pathArray = ctx.resultPath.split('.');
|
|
67
|
-
for (const property of pathArray) {
|
|
68
|
-
result = result && typeof result === 'object' && result[property];
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
if (fnName === 'create')
|
|
72
|
-
ctx.response.status = 201;
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { OpraSchema } from '@opra/schema';
|
|
2
|
-
import { nodeInspectCustom } from '../../utils/terminal-utils.js';
|
|
3
|
-
import { QueryContext } from '../query-context.js';
|
|
4
|
-
export declare abstract class ResourceHandler {
|
|
5
|
-
protected readonly _args: OpraSchema.Resource & {
|
|
6
|
-
prepare?: Function;
|
|
7
|
-
};
|
|
8
|
-
protected constructor(args: OpraSchema.Resource);
|
|
9
|
-
get name(): string;
|
|
10
|
-
get description(): string | undefined;
|
|
11
|
-
toString(): string;
|
|
12
|
-
prepare(ctx: QueryContext): Promise<void>;
|
|
13
|
-
abstract execute(ctx: QueryContext): Promise<void>;
|
|
14
|
-
[nodeInspectCustom](): string;
|
|
15
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom } from '../../utils/terminal-utils.js';
|
|
2
|
-
export class ResourceHandler {
|
|
3
|
-
_args;
|
|
4
|
-
constructor(args) {
|
|
5
|
-
this._args = args;
|
|
6
|
-
}
|
|
7
|
-
get name() {
|
|
8
|
-
return this._args.name;
|
|
9
|
-
}
|
|
10
|
-
get description() {
|
|
11
|
-
return this._args.description;
|
|
12
|
-
}
|
|
13
|
-
toString() {
|
|
14
|
-
return `[${Object.getPrototypeOf(this).constructor.name} ${this.name}]`;
|
|
15
|
-
}
|
|
16
|
-
async prepare(ctx) {
|
|
17
|
-
const { query } = ctx;
|
|
18
|
-
const fn = this._args['pre_' + query.queryType];
|
|
19
|
-
if (fn && typeof fn === 'function') {
|
|
20
|
-
await fn(ctx);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
[nodeInspectCustom]() {
|
|
24
|
-
return `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
|
|
25
|
-
` ${colorFgMagenta + this.name + colorReset}]`;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { StrictOmit, Type } from 'ts-gems';
|
|
2
|
-
import { OpraSchema } from '@opra/schema';
|
|
3
|
-
import { ThunkAsync } from '../types.js';
|
|
4
|
-
export declare namespace SchemaGenerator {
|
|
5
|
-
type GenerateDocumentArgs = StrictOmit<OpraSchema.Document, 'version' | 'types'> & {
|
|
6
|
-
types?: ThunkAsync<Type | OpraSchema.DataType>[];
|
|
7
|
-
};
|
|
8
|
-
type GenerateServiceArgs = StrictOmit<OpraSchema.Service, 'version' | 'types' | 'resources'> & {
|
|
9
|
-
types?: ThunkAsync<Type | OpraSchema.DataType>[];
|
|
10
|
-
resources: any[];
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
export declare class SchemaGenerator {
|
|
14
|
-
protected _dataTypes: Record<string, OpraSchema.DataType>;
|
|
15
|
-
protected _resources: Record<string, OpraSchema.Resource>;
|
|
16
|
-
protected constructor();
|
|
17
|
-
protected addDataType(thunk: ThunkAsync<Type | OpraSchema.DataType>): Promise<OpraSchema.DataType>;
|
|
18
|
-
addResource(instance: any): Promise<void>;
|
|
19
|
-
static generateDocumentSchema(args: SchemaGenerator.GenerateDocumentArgs): Promise<OpraSchema.Document>;
|
|
20
|
-
static generateServiceSchema(args: SchemaGenerator.GenerateServiceArgs): Promise<OpraSchema.Service>;
|
|
21
|
-
}
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { isPromise } from 'util/types';
|
|
2
|
-
import { DATATYPE_METADATA, DATATYPE_PROPERTIES, OpraSchema } from '@opra/schema';
|
|
3
|
-
import { RESOURCE_METADATA } from '../constants.js';
|
|
4
|
-
import { isConstructor, resolveClassAsync } from '../utils/class-utils.js';
|
|
5
|
-
import { builtinClassMap, internalDataTypes, primitiveDataTypeNames } from '../utils/internal-data-types.js';
|
|
6
|
-
var isDataType = OpraSchema.isDataType;
|
|
7
|
-
const entityMethods = ['search', 'count', 'create', 'get', 'update', 'updateMany', 'delete', 'deleteMany'];
|
|
8
|
-
export class SchemaGenerator {
|
|
9
|
-
_dataTypes = {};
|
|
10
|
-
_resources = {};
|
|
11
|
-
constructor() {
|
|
12
|
-
//
|
|
13
|
-
}
|
|
14
|
-
async addDataType(thunk) {
|
|
15
|
-
thunk = isPromise(thunk) ? await thunk : thunk;
|
|
16
|
-
if (typeof thunk === 'function') {
|
|
17
|
-
if (!isConstructor(thunk))
|
|
18
|
-
return this.addDataType(await thunk());
|
|
19
|
-
if (builtinClassMap.has(thunk))
|
|
20
|
-
return this.addDataType(builtinClassMap.get(thunk));
|
|
21
|
-
const ctor = thunk;
|
|
22
|
-
const metadata = Reflect.getOwnMetadata(DATATYPE_METADATA, ctor);
|
|
23
|
-
if (!metadata)
|
|
24
|
-
throw new TypeError(`Class "${ctor}" has no type metadata`);
|
|
25
|
-
// Add base data type
|
|
26
|
-
let base;
|
|
27
|
-
let baseCtor = Object.getPrototypeOf(ctor);
|
|
28
|
-
if (Reflect.hasMetadata(DATATYPE_METADATA, baseCtor)) {
|
|
29
|
-
while (!Reflect.hasOwnMetadata(DATATYPE_METADATA, baseCtor)) {
|
|
30
|
-
baseCtor = Object.getPrototypeOf(baseCtor);
|
|
31
|
-
}
|
|
32
|
-
const baseSchema = await this.addDataType(baseCtor);
|
|
33
|
-
base = baseSchema.name;
|
|
34
|
-
}
|
|
35
|
-
const schema = {
|
|
36
|
-
...metadata,
|
|
37
|
-
ctor,
|
|
38
|
-
base
|
|
39
|
-
};
|
|
40
|
-
const properties = Reflect.getMetadata(DATATYPE_PROPERTIES, ctor.prototype);
|
|
41
|
-
if (properties) {
|
|
42
|
-
for (const [k, p] of Object.entries(properties)) {
|
|
43
|
-
let type = p.type || 'string';
|
|
44
|
-
if (typeof type !== 'string') {
|
|
45
|
-
const propSchema = await this.addDataType(type);
|
|
46
|
-
type = propSchema.name;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
if (internalDataTypes.has(type))
|
|
50
|
-
await this.addDataType(internalDataTypes.get(type));
|
|
51
|
-
}
|
|
52
|
-
schema.properties = schema.properties || {};
|
|
53
|
-
schema.properties[k] = { ...p, type };
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return this.addDataType(schema);
|
|
57
|
-
}
|
|
58
|
-
if (!isDataType(thunk))
|
|
59
|
-
throw new TypeError(`Invalid data type schema`);
|
|
60
|
-
// Check if datatype previously added
|
|
61
|
-
const currentSchema = this._dataTypes[thunk.name];
|
|
62
|
-
if (currentSchema) {
|
|
63
|
-
if (!(currentSchema.kind === thunk.kind && currentSchema.ctor && currentSchema.ctor === thunk.ctor))
|
|
64
|
-
throw new Error(`An other instance of "${currentSchema.name}" data type previously defined`);
|
|
65
|
-
return currentSchema;
|
|
66
|
-
}
|
|
67
|
-
if (OpraSchema.isSimpleType(thunk) && !primitiveDataTypeNames.includes(thunk.type))
|
|
68
|
-
throw new Error(`"type" of SimpleType schema must be one of enumerated value (${primitiveDataTypeNames})`);
|
|
69
|
-
return this._dataTypes[thunk.name] = {
|
|
70
|
-
...thunk
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
async addResource(instance) {
|
|
74
|
-
if (isConstructor(instance))
|
|
75
|
-
throw new Error(`You should provide Resource instance instead of Resource class`);
|
|
76
|
-
const proto = Object.getPrototypeOf(instance);
|
|
77
|
-
const ctor = proto.constructor;
|
|
78
|
-
const metadata = Reflect.getMetadata(RESOURCE_METADATA, ctor);
|
|
79
|
-
let resourceSchema;
|
|
80
|
-
if (metadata) {
|
|
81
|
-
const name = metadata.name || ctor.name.replace(/Resource$/, '');
|
|
82
|
-
const t = typeof metadata.type === 'function'
|
|
83
|
-
? await resolveClassAsync(metadata.type)
|
|
84
|
-
: metadata.type;
|
|
85
|
-
const type = typeof t === 'function'
|
|
86
|
-
? (await this.addDataType(t)).name
|
|
87
|
-
: t;
|
|
88
|
-
resourceSchema = {
|
|
89
|
-
...metadata,
|
|
90
|
-
type,
|
|
91
|
-
name,
|
|
92
|
-
instance
|
|
93
|
-
};
|
|
94
|
-
if (OpraSchema.isEntityResource(resourceSchema)) {
|
|
95
|
-
for (const methodName of entityMethods) {
|
|
96
|
-
let fn = instance['pre_' + methodName];
|
|
97
|
-
if (typeof fn === 'function')
|
|
98
|
-
resourceSchema['pre_' + methodName] = fn.bind(instance);
|
|
99
|
-
fn = instance[methodName];
|
|
100
|
-
if (typeof fn === 'function')
|
|
101
|
-
resourceSchema[methodName] = fn.bind(instance);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
else
|
|
106
|
-
resourceSchema = instance;
|
|
107
|
-
if (OpraSchema.isResource(resourceSchema)) {
|
|
108
|
-
if (OpraSchema.isEntityResource(resourceSchema)) {
|
|
109
|
-
const t = this._dataTypes[resourceSchema.type];
|
|
110
|
-
if (!t)
|
|
111
|
-
throw new Error(`Resource registration error. Type "${resourceSchema.type}" not found.`);
|
|
112
|
-
if (this._resources[resourceSchema.name])
|
|
113
|
-
throw new Error(`An other instance of "${resourceSchema.name}" resource previously defined`);
|
|
114
|
-
this._resources[resourceSchema.name] = resourceSchema;
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
throw new Error(`Invalid resource metadata`);
|
|
118
|
-
}
|
|
119
|
-
throw new Error(`Invalid resource object`);
|
|
120
|
-
}
|
|
121
|
-
static async generateDocumentSchema(args) {
|
|
122
|
-
const generator = new SchemaGenerator();
|
|
123
|
-
if (args.types) {
|
|
124
|
-
for (const thunk of args.types) {
|
|
125
|
-
await generator.addDataType(thunk);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
const types = Object.keys(generator._dataTypes).sort()
|
|
129
|
-
.map(name => generator._dataTypes[name]);
|
|
130
|
-
return {
|
|
131
|
-
version: '1',
|
|
132
|
-
...args,
|
|
133
|
-
types
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
static async generateServiceSchema(args) {
|
|
137
|
-
const generator = new SchemaGenerator();
|
|
138
|
-
if (args.types) {
|
|
139
|
-
for (const thunk of args.types) {
|
|
140
|
-
await generator.addDataType(thunk);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
if (args.resources) {
|
|
144
|
-
for (const resource of args.resources) {
|
|
145
|
-
await generator.addResource(resource);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
const types = Object.keys(generator._dataTypes).sort()
|
|
149
|
-
.map(name => generator._dataTypes[name]);
|
|
150
|
-
const resources = Object.keys(generator._resources).sort()
|
|
151
|
-
.map(name => generator._resources[name]);
|
|
152
|
-
return {
|
|
153
|
-
version: '1',
|
|
154
|
-
...args,
|
|
155
|
-
types,
|
|
156
|
-
resources
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { StrictOmit } from 'ts-gems';
|
|
2
|
-
import { OpraSchema } from '@opra/schema';
|
|
3
|
-
import { TypeThunkAsync } from '../types.js';
|
|
4
|
-
export declare type EntityResourceMetadata = StrictOmit<OpraSchema.EntityResource, 'type' | 'name'> & {
|
|
5
|
-
type: TypeThunkAsync | string;
|
|
6
|
-
name?: string;
|
|
7
|
-
};
|
|
8
|
-
export declare type ResourceReadOperationMetadata = StrictOmit<OpraSchema.ResourceReadOperation, 'handler'>;
|
|
9
|
-
export declare type ResourceSearchOperationMetadata = StrictOmit<OpraSchema.ResourceSearchOperation, 'handler'>;
|
|
10
|
-
export declare type ResourceCreateOperationMetadata = StrictOmit<OpraSchema.ResourceCreateOperation, 'handler'>;
|
|
11
|
-
export declare type ResourceUpdateOperationMetadata = StrictOmit<OpraSchema.ResourceUpdateOperation, 'handler'>;
|
|
12
|
-
export declare type ResourcePatchOperationMetadata = StrictOmit<OpraSchema.ResourcePatchOperation, 'handler'>;
|
|
13
|
-
export declare type ResourceDeleteOperationMetadata = StrictOmit<OpraSchema.ResourceDeleteOperation, 'handler'>;
|
|
14
|
-
export declare type ResourceExecuteOperationMetadata = StrictOmit<OpraSchema.ResourceExecuteOperation, 'handler'>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { EntityResourceHandler } from '../implementation/resource/entity-resource-handler.js';
|
|
2
|
-
import { ResourceHandler } from '../implementation/resource/resource-handler.js';
|
|
3
|
-
export interface IResourceContainer {
|
|
4
|
-
getResource<T extends ResourceHandler>(name: string): T;
|
|
5
|
-
getEntityResource(name: string): EntityResourceHandler;
|
|
6
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Type } from 'ts-gems';
|
|
2
|
-
import { TypeThunk, TypeThunkAsync } from '../types.js';
|
|
3
|
-
export declare function isConstructor(obj: any): obj is Type;
|
|
4
|
-
export declare function resolveClass<T>(typeOrFunc: TypeThunk<T>): Type<T>;
|
|
5
|
-
export declare function resolveClassAsync<T>(typeOrFunc: TypeThunkAsync<T>): Promise<Type<T>>;
|
|
6
|
-
export declare function isInternalClass(t: any): boolean;
|
package/esm/utils/class-utils.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const knownInternalClasses = [String, Number, Boolean, Date];
|
|
2
|
-
export function isConstructor(obj) {
|
|
3
|
-
return typeof obj === 'function' &&
|
|
4
|
-
!!(obj.prototype && obj.prototype.constructor);
|
|
5
|
-
}
|
|
6
|
-
export function resolveClass(typeOrFunc) {
|
|
7
|
-
if (typeof typeOrFunc === 'function' && !isConstructor(typeOrFunc)) {
|
|
8
|
-
typeOrFunc = typeOrFunc();
|
|
9
|
-
if (isConstructor(typeOrFunc))
|
|
10
|
-
return typeOrFunc;
|
|
11
|
-
throw new TypeError(`Function must return a constructor`);
|
|
12
|
-
}
|
|
13
|
-
if (isConstructor(typeOrFunc))
|
|
14
|
-
return typeOrFunc;
|
|
15
|
-
throw new TypeError(`${typeOrFunc} is not a constructor`);
|
|
16
|
-
}
|
|
17
|
-
export async function resolveClassAsync(typeOrFunc) {
|
|
18
|
-
if (typeof typeOrFunc === 'function' && !isConstructor(typeOrFunc)) {
|
|
19
|
-
typeOrFunc = await typeOrFunc();
|
|
20
|
-
if (isConstructor(typeOrFunc))
|
|
21
|
-
return typeOrFunc;
|
|
22
|
-
throw new TypeError(`Function must return a constructor`);
|
|
23
|
-
}
|
|
24
|
-
if (isConstructor(typeOrFunc))
|
|
25
|
-
return typeOrFunc;
|
|
26
|
-
throw new TypeError(`${typeOrFunc} is not a constructor`);
|
|
27
|
-
}
|
|
28
|
-
export function isInternalClass(t) {
|
|
29
|
-
return knownInternalClasses.includes(t) || Buffer.isBuffer(t);
|
|
30
|
-
}
|
package/esm/utils/headers.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Opaque } from 'ts-gems';
|
|
2
|
-
export declare type HeadersObject = Opaque<Record<string, any>, 'HeadersObject'>;
|
|
3
|
-
/**
|
|
4
|
-
* Create a proxy that ensures header keys are always formatted according to well known header names
|
|
5
|
-
*/
|
|
6
|
-
export declare namespace Headers {
|
|
7
|
-
function from(obj: object): HeadersObject;
|
|
8
|
-
function create(): HeadersObject;
|
|
9
|
-
}
|
package/esm/utils/headers.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { HttpHeaders } from '../enums/index.js';
|
|
2
|
-
const wellKnownHeaders = Object.values(HttpHeaders)
|
|
3
|
-
.reduce((target, k) => {
|
|
4
|
-
target[k.toLowerCase()] = k;
|
|
5
|
-
return target;
|
|
6
|
-
}, {});
|
|
7
|
-
const wrapKey = (prop) => {
|
|
8
|
-
return typeof prop == 'string' ? wellKnownHeaders[prop.toLowerCase()] || prop : prop;
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* Create a proxy that ensures header keys are always formatted according to well known header names
|
|
12
|
-
*/
|
|
13
|
-
export var Headers;
|
|
14
|
-
(function (Headers) {
|
|
15
|
-
function from(obj) {
|
|
16
|
-
const headers = create();
|
|
17
|
-
for (const [k, v] of Object.entries(obj))
|
|
18
|
-
headers[k] = v;
|
|
19
|
-
return headers;
|
|
20
|
-
}
|
|
21
|
-
Headers.from = from;
|
|
22
|
-
function create() {
|
|
23
|
-
return new Proxy({}, {
|
|
24
|
-
set: (target, prop, value, receiver) => {
|
|
25
|
-
let key = prop;
|
|
26
|
-
if (typeof key === 'string')
|
|
27
|
-
key = wellKnownHeaders[key.toLowerCase()] || key;
|
|
28
|
-
const result = Reflect.set(target, key, value, receiver);
|
|
29
|
-
/* istanbul ignore next */
|
|
30
|
-
if (!result && Object.isFrozen(target))
|
|
31
|
-
throw new TypeError('Cannot add property, object is not extensible');
|
|
32
|
-
return result;
|
|
33
|
-
},
|
|
34
|
-
get: (target, prop, receiver) => {
|
|
35
|
-
return Reflect.get(target, wrapKey(prop), receiver);
|
|
36
|
-
},
|
|
37
|
-
deleteProperty: (target, prop) => {
|
|
38
|
-
return Reflect.deleteProperty(target, wrapKey(prop));
|
|
39
|
-
},
|
|
40
|
-
defineProperty: (target, prop, descriptor) => {
|
|
41
|
-
let key = prop;
|
|
42
|
-
if (typeof key === 'string')
|
|
43
|
-
key = wellKnownHeaders[key.toLowerCase()] || key;
|
|
44
|
-
return Reflect.defineProperty(target, key, descriptor);
|
|
45
|
-
},
|
|
46
|
-
getOwnPropertyDescriptor: (target, prop) => {
|
|
47
|
-
return Reflect.getOwnPropertyDescriptor(target, wrapKey(prop));
|
|
48
|
-
},
|
|
49
|
-
has: (target, prop) => {
|
|
50
|
-
return Reflect.has(target, wrapKey(prop));
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
Headers.create = create;
|
|
55
|
-
})(Headers || (Headers = {}));
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Type } from 'ts-gems';
|
|
2
|
-
import { OpraSchema } from '@opra/schema';
|
|
3
|
-
export declare const primitiveDataTypeNames: string[];
|
|
4
|
-
export declare const builtinClassMap: Map<Type<any>, OpraSchema.DataType>;
|
|
5
|
-
export declare const internalDataTypes: Map<string, OpraSchema.DataType>;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
export const primitiveDataTypeNames = ['boolean', 'number', 'string', 'null'];
|
|
2
|
-
export const builtinClassMap = new Map();
|
|
3
|
-
export const internalDataTypes = new Map();
|
|
4
|
-
const internalDataTypeArray = [
|
|
5
|
-
{
|
|
6
|
-
kind: 'SimpleType',
|
|
7
|
-
name: 'boolean',
|
|
8
|
-
type: 'boolean',
|
|
9
|
-
description: 'Simple true/false value',
|
|
10
|
-
ctor: Boolean
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
kind: 'SimpleType',
|
|
14
|
-
name: 'string',
|
|
15
|
-
type: 'string',
|
|
16
|
-
description: 'A sequence of characters',
|
|
17
|
-
ctor: String
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
kind: 'SimpleType',
|
|
21
|
-
name: 'number',
|
|
22
|
-
type: 'number',
|
|
23
|
-
description: 'Both Integer as well as Floating-Point numbers',
|
|
24
|
-
ctor: Number
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
kind: 'SimpleType',
|
|
28
|
-
name: 'integer',
|
|
29
|
-
type: 'number',
|
|
30
|
-
base: 'number',
|
|
31
|
-
description: 'Integer number',
|
|
32
|
-
ctor: Number
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
kind: 'ComplexType',
|
|
36
|
-
name: 'object',
|
|
37
|
-
description: 'Object type with additional properties',
|
|
38
|
-
additionalProperties: true,
|
|
39
|
-
ctor: Object
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
kind: 'SimpleType',
|
|
43
|
-
name: 'date',
|
|
44
|
-
type: 'string',
|
|
45
|
-
description: 'Date',
|
|
46
|
-
format: 'date',
|
|
47
|
-
ctor: Date
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
kind: 'SimpleType',
|
|
51
|
-
name: 'date-time',
|
|
52
|
-
type: 'string',
|
|
53
|
-
description: 'Date time value',
|
|
54
|
-
format: 'date-time',
|
|
55
|
-
ctor: Date
|
|
56
|
-
},
|
|
57
|
-
{
|
|
58
|
-
kind: 'SimpleType',
|
|
59
|
-
name: 'time',
|
|
60
|
-
type: 'string',
|
|
61
|
-
description: 'Time value',
|
|
62
|
-
format: 'time',
|
|
63
|
-
ctor: String
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
kind: 'SimpleType',
|
|
67
|
-
name: 'buffer',
|
|
68
|
-
type: 'string',
|
|
69
|
-
description: 'Buffer value',
|
|
70
|
-
format: 'base64',
|
|
71
|
-
ctor: Buffer
|
|
72
|
-
}
|
|
73
|
-
];
|
|
74
|
-
internalDataTypeArray.forEach(sch => {
|
|
75
|
-
internalDataTypes.set(sch.name, sch);
|
|
76
|
-
if (sch.ctor && !builtinClassMap.has(sch.ctor))
|
|
77
|
-
builtinClassMap.set(sch.ctor, sch);
|
|
78
|
-
});
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
export function Responsive(wrapped) {
|
|
2
|
-
wrapped = wrapped || {};
|
|
3
|
-
const keyMap = {};
|
|
4
|
-
Object.keys(wrapped).forEach(k => keyMap[k.toLowerCase()] = k);
|
|
5
|
-
const wrapKey = (prop) => {
|
|
6
|
-
return typeof prop == 'string' ? keyMap[prop.toLowerCase()] : prop;
|
|
7
|
-
};
|
|
8
|
-
return new Proxy(wrapped, {
|
|
9
|
-
set: (target, prop, value, receiver) => {
|
|
10
|
-
let key = prop;
|
|
11
|
-
if (typeof key === 'string') {
|
|
12
|
-
const keyLower = key.toLowerCase();
|
|
13
|
-
key = keyMap[keyLower] = keyMap[keyLower] || key;
|
|
14
|
-
}
|
|
15
|
-
const result = Reflect.set(target, key, value, receiver);
|
|
16
|
-
if (!result && Object.isFrozen(target))
|
|
17
|
-
throw new TypeError('Cannot add property, object is not extensible');
|
|
18
|
-
return result;
|
|
19
|
-
},
|
|
20
|
-
get: (target, prop, receiver) => {
|
|
21
|
-
return Reflect.get(target, wrapKey(prop), receiver);
|
|
22
|
-
},
|
|
23
|
-
deleteProperty: (target, prop) => {
|
|
24
|
-
const key = wrapKey(prop);
|
|
25
|
-
if (typeof prop === 'string') {
|
|
26
|
-
delete keyMap[prop.toLowerCase()];
|
|
27
|
-
}
|
|
28
|
-
return Reflect.deleteProperty(target, key);
|
|
29
|
-
},
|
|
30
|
-
defineProperty: (target, prop, descriptor) => {
|
|
31
|
-
let key = prop;
|
|
32
|
-
if (typeof key === 'string') {
|
|
33
|
-
const keyLower = key.toLowerCase();
|
|
34
|
-
key = keyMap[keyLower] = keyMap[keyLower] || key;
|
|
35
|
-
}
|
|
36
|
-
return Reflect.defineProperty(target, key, descriptor);
|
|
37
|
-
},
|
|
38
|
-
getOwnPropertyDescriptor: (target, prop) => {
|
|
39
|
-
return Reflect.getOwnPropertyDescriptor(target, wrapKey(prop));
|
|
40
|
-
},
|
|
41
|
-
has: (target, prop) => {
|
|
42
|
-
return Reflect.has(target, wrapKey(prop));
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}
|