@opra/core 0.22.0 → 0.23.1
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/adapter/execution-context.host.js +48 -0
- package/cjs/adapter/http/express-adapter.host.js +24 -0
- package/cjs/adapter/http/express-adapter.js +12 -45
- package/cjs/adapter/http/helpers/concat-readable.js +20 -0
- package/cjs/adapter/http/helpers/multipart-helper.js +96 -0
- package/cjs/adapter/http/helpers/query-parsers.js +16 -0
- package/cjs/adapter/http/http-adapter-base.js +127 -0
- package/cjs/adapter/http/http-adapter.host.js +57 -0
- package/cjs/adapter/http/http-adapter.js +11 -133
- package/cjs/adapter/http/{impl/http-server-request.js → http-server-request.js} +11 -5
- package/cjs/adapter/http/{impl/http-server-response.js → http-server-response.js} +4 -4
- package/cjs/adapter/http/impl/http-incoming-message.host.js +148 -0
- package/cjs/adapter/http/impl/{http-outgoing-message-host.js → http-outgoing-message.host.js} +26 -38
- package/cjs/adapter/http/request-handlers/entity-request-handler.js +405 -0
- package/cjs/adapter/http/request-handlers/request-handler-base.js +27 -0
- package/cjs/adapter/http/request-handlers/storage-request-handler.js +134 -0
- package/cjs/adapter/operation-context.js +16 -0
- package/cjs/adapter/platform-adapter.host.js +107 -0
- package/cjs/adapter/request.host.js +1 -2
- package/cjs/adapter/request.js +2 -0
- package/cjs/adapter/response.js +2 -0
- package/cjs/adapter/services/logger.js +36 -0
- package/cjs/augmentation/collection.augmentation.js +2 -0
- package/cjs/augmentation/singleton.augmentation.js +2 -0
- package/cjs/augmentation/storage.augmentation.js +2 -0
- package/cjs/index.js +15 -9
- package/esm/adapter/execution-context.host.js +44 -0
- package/esm/adapter/http/express-adapter.host.js +20 -0
- package/esm/adapter/http/express-adapter.js +11 -20
- package/esm/adapter/http/helpers/concat-readable.js +16 -0
- package/esm/adapter/http/helpers/multipart-helper.js +91 -0
- package/esm/adapter/http/helpers/query-parsers.js +12 -0
- package/esm/adapter/http/http-adapter-base.js +123 -0
- package/esm/adapter/http/http-adapter.host.js +52 -0
- package/esm/adapter/http/http-adapter.js +11 -132
- package/esm/adapter/http/{impl/http-server-request.js → http-server-request.js} +12 -6
- package/esm/adapter/http/{impl/http-server-response.js → http-server-response.js} +4 -4
- package/esm/adapter/http/impl/http-incoming-message.host.js +144 -0
- package/esm/adapter/http/impl/{http-outgoing-message-host.js → http-outgoing-message.host.js} +25 -36
- package/esm/adapter/http/request-handlers/entity-request-handler.js +400 -0
- package/esm/adapter/http/request-handlers/request-handler-base.js +23 -0
- package/esm/adapter/http/request-handlers/storage-request-handler.js +129 -0
- package/esm/adapter/operation-context.js +13 -0
- package/esm/adapter/platform-adapter.host.js +102 -0
- package/esm/adapter/request.host.js +1 -2
- package/esm/adapter/request.js +1 -0
- package/esm/adapter/response.js +1 -0
- package/esm/adapter/services/logger.js +32 -0
- package/esm/augmentation/collection.augmentation.js +1 -0
- package/esm/augmentation/singleton.augmentation.js +1 -0
- package/esm/augmentation/storage.augmentation.js +1 -0
- package/esm/index.js +15 -9
- package/i18n/en/error.json +7 -2
- package/package.json +7 -6
- package/types/adapter/execution-context.d.ts +31 -0
- package/types/adapter/execution-context.host.d.ts +27 -0
- package/types/adapter/http/express-adapter.d.ts +12 -8
- package/types/adapter/http/express-adapter.host.d.ts +11 -0
- package/types/adapter/http/helpers/concat-readable.d.ts +3 -0
- package/types/adapter/http/helpers/multipart-helper.d.ts +25 -0
- package/types/adapter/http/helpers/query-parsers.d.ts +1 -0
- package/types/adapter/http/http-adapter-base.d.ts +23 -0
- package/types/adapter/http/http-adapter.d.ts +13 -29
- package/types/adapter/http/http-adapter.host.d.ts +18 -0
- package/types/adapter/http/{impl/http-server-request.d.ts → http-server-request.d.ts} +7 -6
- package/types/adapter/http/{impl/http-server-response.d.ts → http-server-response.d.ts} +2 -2
- package/types/adapter/http/impl/{http-incoming-message-host.d.ts → http-incoming-message.host.d.ts} +16 -12
- package/types/adapter/http/impl/{http-outgoing-message-host.d.ts → http-outgoing-message.host.d.ts} +12 -16
- package/types/adapter/http/request-handlers/entity-request-handler.d.ts +24 -0
- package/types/adapter/http/request-handlers/request-handler-base.d.ts +15 -0
- package/types/adapter/http/request-handlers/storage-request-handler.d.ts +23 -0
- package/types/adapter/interfaces/logger.interface.d.ts +7 -6
- package/types/adapter/interfaces/request-handler.interface.d.ts +4 -0
- package/types/adapter/operation-context.d.ts +11 -0
- package/types/adapter/{adapter.d.ts → platform-adapter.d.ts} +18 -28
- package/types/adapter/platform-adapter.host.d.ts +31 -0
- package/types/adapter/request.d.ts +11 -0
- package/types/adapter/request.host.d.ts +12 -21
- package/types/adapter/{interfaces/response.interface.d.ts → response.d.ts} +2 -2
- package/types/adapter/response.host.d.ts +2 -2
- package/types/adapter/services/logger.d.ts +14 -0
- package/types/augmentation/collection.augmentation.d.ts +112 -0
- package/types/augmentation/singleton.augmentation.d.ts +64 -0
- package/types/augmentation/storage.augmentation.d.ts +39 -0
- package/types/index.d.ts +15 -9
- package/cjs/adapter/adapter.js +0 -118
- package/cjs/adapter/http/impl/http-incoming-message-host.js +0 -127
- package/cjs/adapter/http/request-parsers/parse-collection-request.js +0 -165
- package/cjs/adapter/http/request-parsers/parse-request.js +0 -24
- package/cjs/adapter/http/request-parsers/parse-singleton-request.js +0 -96
- package/cjs/adapter/internal/metadata.resource.js +0 -26
- package/cjs/adapter/request-context.host.js +0 -44
- package/cjs/shared/collection-resource-base.js +0 -20
- package/esm/adapter/adapter.js +0 -113
- package/esm/adapter/http/impl/http-incoming-message-host.js +0 -122
- package/esm/adapter/http/request-parsers/parse-collection-request.js +0 -161
- package/esm/adapter/http/request-parsers/parse-request.js +0 -20
- package/esm/adapter/http/request-parsers/parse-singleton-request.js +0 -92
- package/esm/adapter/internal/metadata.resource.js +0 -23
- package/esm/adapter/request-context.host.js +0 -40
- package/esm/shared/collection-resource-base.js +0 -16
- package/types/adapter/http/request-parsers/parse-collection-request.d.ts +0 -4
- package/types/adapter/http/request-parsers/parse-request.d.ts +0 -4
- package/types/adapter/http/request-parsers/parse-singleton-request.d.ts +0 -4
- package/types/adapter/interfaces/request-context.interface.d.ts +0 -31
- package/types/adapter/interfaces/request.interface.d.ts +0 -15
- package/types/adapter/internal/metadata.resource.d.ts +0 -7
- package/types/adapter/request-context.host.d.ts +0 -22
- package/types/shared/collection-resource-base.d.ts +0 -11
- /package/cjs/adapter/{interfaces/request-context.interface.js → execution-context.js} +0 -0
- /package/cjs/adapter/http/{request-parsers/batch-request-parser.js → request-handlers/parse-batch-request.js} +0 -0
- /package/cjs/adapter/interfaces/{request.interface.js → request-handler.interface.js} +0 -0
- /package/cjs/adapter/{interfaces/response.interface.js → platform-adapter.js} +0 -0
- /package/esm/adapter/{interfaces/request-context.interface.js → execution-context.js} +0 -0
- /package/esm/adapter/http/{request-parsers/batch-request-parser.js → request-handlers/parse-batch-request.js} +0 -0
- /package/esm/adapter/interfaces/{request.interface.js → request-handler.interface.js} +0 -0
- /package/esm/adapter/{interfaces/response.interface.js → platform-adapter.js} +0 -0
- /package/types/adapter/http/{request-parsers/batch-request-parser.d.ts → request-handlers/parse-batch-request.d.ts} +0 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ExecutionContext } from './execution-context';
|
|
2
|
+
import type { Request } from './request';
|
|
3
|
+
import type { Response } from './response';
|
|
4
|
+
export interface OperationContext<TRequest extends Request = Request, TResponse extends Response = Response> extends ExecutionContext {
|
|
5
|
+
request: TRequest;
|
|
6
|
+
response: TResponse;
|
|
7
|
+
requestScope: Record<string | number | symbol, any>;
|
|
8
|
+
}
|
|
9
|
+
export declare namespace OperationContext {
|
|
10
|
+
function from<TRequest extends Request = Request, TResponse extends Response = Response>(executionContext: ExecutionContext, request: TRequest, response: TResponse): OperationContext;
|
|
11
|
+
}
|
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ApiDocument, FallbackLng, I18n, LanguageResource } from '@opra/common';
|
|
1
|
+
import { FallbackLng, I18n, LanguageResource } from '@opra/common';
|
|
3
2
|
import { ILogger } from './interfaces/logger.interface.js';
|
|
4
|
-
import {
|
|
3
|
+
import { RequestHandler } from './interfaces/request-handler.interface.js';
|
|
4
|
+
import { OperationContext } from './operation-context.js';
|
|
5
|
+
export type Protocol = 'http' | 'ws' | 'rpc';
|
|
6
|
+
export interface PlatformAdapter extends RequestHandler {
|
|
7
|
+
readonly protocol: Protocol;
|
|
8
|
+
readonly platform: string;
|
|
9
|
+
readonly serviceName: string;
|
|
10
|
+
/**
|
|
11
|
+
* Calls shutDown hook for all resources
|
|
12
|
+
*/
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
}
|
|
5
15
|
/**
|
|
6
|
-
* @namespace
|
|
16
|
+
* @namespace PlatformAdapter
|
|
7
17
|
*/
|
|
8
|
-
export declare namespace
|
|
18
|
+
export declare namespace PlatformAdapter {
|
|
9
19
|
interface Options {
|
|
10
20
|
i18n?: I18n | I18nOptions | (() => Promise<I18n>);
|
|
11
21
|
logger?: ILogger;
|
|
12
|
-
|
|
22
|
+
on?: {
|
|
23
|
+
request?: (ctx: OperationContext) => void | Promise<void>;
|
|
24
|
+
};
|
|
13
25
|
}
|
|
14
26
|
interface I18nOptions {
|
|
15
27
|
/**
|
|
@@ -39,25 +51,3 @@ export declare namespace OpraAdapter {
|
|
|
39
51
|
resourceDirs?: string[];
|
|
40
52
|
}
|
|
41
53
|
}
|
|
42
|
-
/**
|
|
43
|
-
* @class OpraAdapter
|
|
44
|
-
*/
|
|
45
|
-
export declare abstract class OpraAdapter extends AsyncEventEmitter {
|
|
46
|
-
readonly api: ApiDocument;
|
|
47
|
-
protected _apiRoot: ApiDocument;
|
|
48
|
-
i18n: I18n;
|
|
49
|
-
logger?: ILogger;
|
|
50
|
-
protected constructor(api: ApiDocument);
|
|
51
|
-
/**
|
|
52
|
-
* Initializes the adapter
|
|
53
|
-
* @param options
|
|
54
|
-
* @protected
|
|
55
|
-
*/
|
|
56
|
-
protected init(options?: OpraAdapter.Options): Promise<void>;
|
|
57
|
-
/**
|
|
58
|
-
* Calls shutDown hook for all resources
|
|
59
|
-
*/
|
|
60
|
-
close(): Promise<void>;
|
|
61
|
-
protected executeRequest(context: RequestContext): Promise<void>;
|
|
62
|
-
protected _createI18n(options?: OpraAdapter.I18nOptions): Promise<I18n>;
|
|
63
|
-
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AsyncEventEmitter } from 'strict-typed-events';
|
|
2
|
+
import { ApiDocument, I18n, Resource } from '@opra/common';
|
|
3
|
+
import { ExecutionContext } from './execution-context.js';
|
|
4
|
+
import type { PlatformAdapter, Protocol } from './platform-adapter';
|
|
5
|
+
import { Logger } from './services/logger.js';
|
|
6
|
+
/**
|
|
7
|
+
* @class PlatformAdapterHost
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class PlatformAdapterHost extends AsyncEventEmitter implements PlatformAdapter {
|
|
10
|
+
readonly api: ApiDocument;
|
|
11
|
+
_controllers: WeakMap<Resource, any>;
|
|
12
|
+
_protocol: Protocol;
|
|
13
|
+
_platform: string;
|
|
14
|
+
_initialized: boolean;
|
|
15
|
+
_serviceName: string;
|
|
16
|
+
_options: PlatformAdapter.Options;
|
|
17
|
+
_i18n: I18n;
|
|
18
|
+
_logger: Logger;
|
|
19
|
+
protected constructor(api: ApiDocument, options?: PlatformAdapter.Options);
|
|
20
|
+
get platform(): string;
|
|
21
|
+
get protocol(): Protocol;
|
|
22
|
+
get serviceName(): string;
|
|
23
|
+
close(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Initializes the adapter
|
|
26
|
+
*/
|
|
27
|
+
init(): Promise<void>;
|
|
28
|
+
getController(resource: Resource | string): Promise<any>;
|
|
29
|
+
protected _createI18n(options?: PlatformAdapter.I18nOptions): Promise<I18n>;
|
|
30
|
+
abstract processRequest(executionContext: ExecutionContext): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Resource } from '@opra/common';
|
|
2
|
+
import { HttpServerRequest } from './http/http-server-request.js';
|
|
3
|
+
export interface Request {
|
|
4
|
+
resource: Resource;
|
|
5
|
+
operation: string;
|
|
6
|
+
contentId?: string;
|
|
7
|
+
params?: Record<string, any>;
|
|
8
|
+
switchToHttp(): HttpServerRequest;
|
|
9
|
+
switchToWs(): never;
|
|
10
|
+
switchToRpc(): never;
|
|
11
|
+
}
|
|
@@ -1,29 +1,20 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type { Request } from './interfaces/request.interface.js';
|
|
1
|
+
import type { StrictOmit } from 'ts-gems';
|
|
2
|
+
import type { HttpServerRequest } from './http/http-server-request.js';
|
|
3
|
+
import type { Request } from './request';
|
|
5
4
|
export declare namespace RequestHost {
|
|
6
|
-
interface Initiator {
|
|
7
|
-
|
|
8
|
-
kind: string;
|
|
9
|
-
resource: Resource;
|
|
10
|
-
operation: string;
|
|
11
|
-
crud: 'create' | 'read' | 'update' | 'delete';
|
|
12
|
-
many: boolean;
|
|
13
|
-
args: any;
|
|
5
|
+
interface Initiator extends StrictOmit<Request, 'switchToHttp' | 'switchToWs' | 'switchToRpc'> {
|
|
6
|
+
controller: any;
|
|
14
7
|
http?: HttpServerRequest;
|
|
8
|
+
[key: string]: any;
|
|
15
9
|
}
|
|
16
10
|
}
|
|
11
|
+
export interface RequestHost extends Request {
|
|
12
|
+
}
|
|
17
13
|
export declare class RequestHost implements Request {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
readonly operation: string;
|
|
23
|
-
readonly crud: 'create' | 'read' | 'update' | 'delete';
|
|
24
|
-
readonly many: boolean;
|
|
25
|
-
readonly args: any;
|
|
26
|
-
readonly http?: HttpServerRequest;
|
|
14
|
+
controller: any;
|
|
15
|
+
http?: HttpServerRequest;
|
|
16
|
+
key?: any;
|
|
17
|
+
params: Record<string, any>;
|
|
27
18
|
constructor(init: RequestHost.Initiator);
|
|
28
19
|
switchToHttp(): HttpServerRequest;
|
|
29
20
|
switchToWs(): never;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HttpServerResponse } from '
|
|
1
|
+
import type { HttpServerResponse } from './http/http-server-response';
|
|
2
2
|
export interface Response {
|
|
3
3
|
/**
|
|
4
4
|
* Result value
|
|
@@ -7,7 +7,7 @@ export interface Response {
|
|
|
7
7
|
/**
|
|
8
8
|
* List of errors
|
|
9
9
|
*/
|
|
10
|
-
errors:
|
|
10
|
+
errors: any[];
|
|
11
11
|
/**
|
|
12
12
|
*
|
|
13
13
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HttpServerResponse } from './http/
|
|
2
|
-
import { Response } from './
|
|
1
|
+
import type { HttpServerResponse } from './http/http-server-response';
|
|
2
|
+
import type { Response } from './response';
|
|
3
3
|
export declare namespace ResponseHost {
|
|
4
4
|
interface Initiator {
|
|
5
5
|
value?: any;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ILogger } from '../interfaces/logger.interface.js';
|
|
2
|
+
export interface LoggerOptions {
|
|
3
|
+
instance?: ILogger;
|
|
4
|
+
}
|
|
5
|
+
export declare class Logger implements ILogger {
|
|
6
|
+
protected _instance: ILogger;
|
|
7
|
+
constructor(options?: LoggerOptions);
|
|
8
|
+
info(message: any, ...optionalParams: any[]): void;
|
|
9
|
+
error(message: any, ...optionalParams: any[]): void;
|
|
10
|
+
fatal(message: any, ...optionalParams: any[]): void;
|
|
11
|
+
warn(message: any, ...optionalParams: any[]): void;
|
|
12
|
+
debug(message: any, ...optionalParams: any[]): void;
|
|
13
|
+
verbose(message: any, ...optionalParams: any[]): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { PartialOutput } from '@opra/common';
|
|
2
|
+
import type { OperationContext } from '../adapter/operation-context.js';
|
|
3
|
+
import type { Request as _Request } from '../adapter/request.js';
|
|
4
|
+
declare module "@opra/common" {
|
|
5
|
+
namespace Collection {
|
|
6
|
+
namespace Create {
|
|
7
|
+
interface Request extends _Request {
|
|
8
|
+
operation: 'create';
|
|
9
|
+
data: any;
|
|
10
|
+
params: {
|
|
11
|
+
pick?: string[];
|
|
12
|
+
omit?: string[];
|
|
13
|
+
include?: string[];
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
interface Context extends OperationContext {
|
|
17
|
+
request: Request;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
namespace Delete {
|
|
21
|
+
interface Request extends _Request {
|
|
22
|
+
operation: 'delete';
|
|
23
|
+
key: any;
|
|
24
|
+
}
|
|
25
|
+
interface Context extends OperationContext {
|
|
26
|
+
request: Request;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
namespace DeleteMany {
|
|
30
|
+
interface Request extends _Request {
|
|
31
|
+
operation: 'deleteMany';
|
|
32
|
+
params: {
|
|
33
|
+
filter?: any;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
interface Context extends OperationContext {
|
|
37
|
+
request: Request;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
namespace FindMany {
|
|
41
|
+
interface Request extends _Request {
|
|
42
|
+
operation: 'findMany';
|
|
43
|
+
params: {
|
|
44
|
+
filter?: any;
|
|
45
|
+
pick?: string[];
|
|
46
|
+
omit?: string[];
|
|
47
|
+
include?: string[];
|
|
48
|
+
sort?: string[];
|
|
49
|
+
limit?: number;
|
|
50
|
+
skip?: number;
|
|
51
|
+
distinct?: boolean;
|
|
52
|
+
count?: boolean;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
interface Context extends OperationContext {
|
|
56
|
+
request: Request;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
namespace Get {
|
|
60
|
+
interface Request extends _Request {
|
|
61
|
+
operation: 'get';
|
|
62
|
+
key: any;
|
|
63
|
+
params: {
|
|
64
|
+
pick?: string[];
|
|
65
|
+
omit?: string[];
|
|
66
|
+
include?: string[];
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
interface Context extends OperationContext {
|
|
70
|
+
request: Request;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
namespace Update {
|
|
74
|
+
interface Request extends _Request {
|
|
75
|
+
operation: 'update';
|
|
76
|
+
key: any;
|
|
77
|
+
data: any;
|
|
78
|
+
params: {
|
|
79
|
+
pick?: string[];
|
|
80
|
+
omit?: string[];
|
|
81
|
+
include?: string[];
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
interface Context extends OperationContext {
|
|
85
|
+
request: Request;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
namespace UpdateMany {
|
|
89
|
+
interface Request extends _Request {
|
|
90
|
+
operation: 'updateMany';
|
|
91
|
+
data: any;
|
|
92
|
+
params: {
|
|
93
|
+
filter?: any;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
interface Context extends OperationContext {
|
|
97
|
+
request: Request;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
interface CollectionResource<T> {
|
|
102
|
+
create?(context: Collection.Create.Context): Promise<PartialOutput<T>>;
|
|
103
|
+
delete?(context: Collection.Delete.Context): Promise<number> | undefined;
|
|
104
|
+
deleteMany?(context: Collection.DeleteMany.Context): Promise<number> | undefined;
|
|
105
|
+
findMany?(context: Collection.FindMany.Context): Promise<PartialOutput<T>[] | undefined>;
|
|
106
|
+
get?(context: Collection.Get.Context): Promise<PartialOutput<T> | undefined>;
|
|
107
|
+
update?(context: Collection.Update.Context): Promise<PartialOutput<T> | undefined>;
|
|
108
|
+
updateMany?(context: Collection.UpdateMany.Context): Promise<number> | undefined;
|
|
109
|
+
onInit?(): Promise<void>;
|
|
110
|
+
onShutdown?(): Promise<void>;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { PartialOutput } from '@opra/common';
|
|
2
|
+
import type { OperationContext } from '../adapter/operation-context.js';
|
|
3
|
+
import type { Request as _Request } from '../adapter/request.js';
|
|
4
|
+
declare module "@opra/common" {
|
|
5
|
+
namespace Singleton {
|
|
6
|
+
namespace Create {
|
|
7
|
+
interface Request extends _Request {
|
|
8
|
+
operation: 'create';
|
|
9
|
+
data: any;
|
|
10
|
+
params: {
|
|
11
|
+
pick?: string[];
|
|
12
|
+
omit?: string[];
|
|
13
|
+
include?: string[];
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
interface Context extends OperationContext {
|
|
17
|
+
request: Request;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
namespace Delete {
|
|
21
|
+
interface Request extends _Request {
|
|
22
|
+
operation: 'delete';
|
|
23
|
+
}
|
|
24
|
+
interface Context extends OperationContext {
|
|
25
|
+
request: Request;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
namespace Get {
|
|
29
|
+
interface Request extends _Request {
|
|
30
|
+
operation: 'get';
|
|
31
|
+
params: {
|
|
32
|
+
pick?: string[];
|
|
33
|
+
omit?: string[];
|
|
34
|
+
include?: string[];
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
interface Context extends OperationContext {
|
|
38
|
+
request: Request;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
namespace Update {
|
|
42
|
+
interface Request extends _Request {
|
|
43
|
+
operation: 'update';
|
|
44
|
+
data: any;
|
|
45
|
+
params: {
|
|
46
|
+
pick?: string[];
|
|
47
|
+
omit?: string[];
|
|
48
|
+
include?: string[];
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
interface Context extends OperationContext {
|
|
52
|
+
request: Request;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
interface SingletonResource<T> {
|
|
57
|
+
create?(context: Singleton.Create.Context): Promise<PartialOutput<T>>;
|
|
58
|
+
delete?(context: Singleton.Delete.Context): Promise<number> | undefined;
|
|
59
|
+
get?(context: Singleton.Get.Context): Promise<PartialOutput<T> | undefined>;
|
|
60
|
+
update?(context: Singleton.Update.Context): Promise<PartialOutput<T> | undefined>;
|
|
61
|
+
onInit?(): Promise<void>;
|
|
62
|
+
onShutdown?(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import type { Readable } from 'stream';
|
|
4
|
+
import type { MultipartIterator } from '../adapter/http/helpers/multipart-helper';
|
|
5
|
+
import type { OperationContext } from '../adapter/operation-context.js';
|
|
6
|
+
import type { Request as _Request } from '../adapter/request.js';
|
|
7
|
+
declare module "@opra/common" {
|
|
8
|
+
namespace Storage {
|
|
9
|
+
namespace Delete {
|
|
10
|
+
interface Request extends _Request {
|
|
11
|
+
}
|
|
12
|
+
interface Context extends OperationContext {
|
|
13
|
+
request: Request;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
namespace Get {
|
|
17
|
+
interface Request extends _Request {
|
|
18
|
+
}
|
|
19
|
+
interface Context extends OperationContext {
|
|
20
|
+
request: Request;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
namespace Post {
|
|
24
|
+
interface Request extends _Request {
|
|
25
|
+
parts: MultipartIterator;
|
|
26
|
+
}
|
|
27
|
+
interface Context extends OperationContext {
|
|
28
|
+
request: Request;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
interface StorageResource {
|
|
33
|
+
delete?(context: Storage.Delete.Context): Promise<number | undefined>;
|
|
34
|
+
get?(context: Storage.Get.Context): Promise<Buffer | Readable | undefined>;
|
|
35
|
+
post?(context: Storage.Post.Context): Promise<void>;
|
|
36
|
+
onInit?(): Promise<void>;
|
|
37
|
+
onShutdown?(): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import './augmentation/resource.augmentation.js';
|
|
3
|
+
import './augmentation/collection.augmentation.js';
|
|
4
|
+
import './augmentation/singleton.augmentation.js';
|
|
5
|
+
import './augmentation/storage.augmentation.js';
|
|
3
6
|
export * from './types.js';
|
|
4
|
-
export * from './adapter/
|
|
7
|
+
export * from './adapter/execution-context.js';
|
|
8
|
+
export * from './adapter/operation-context.js';
|
|
9
|
+
export * from './adapter/platform-adapter.js';
|
|
10
|
+
export * from './adapter/request.js';
|
|
11
|
+
export * from './adapter/response.js';
|
|
5
12
|
export * from './adapter/http/express-adapter.js';
|
|
6
13
|
export * from './adapter/http/http-adapter.js';
|
|
7
|
-
export * from './adapter/http/impl/http-
|
|
8
|
-
export * from './adapter/http/impl/http-
|
|
9
|
-
export * from './adapter/http/
|
|
10
|
-
export * from './adapter/http/
|
|
11
|
-
export * from './adapter/interfaces/request-context.interface.js';
|
|
14
|
+
export * from './adapter/http/impl/http-incoming-message.host.js';
|
|
15
|
+
export * from './adapter/http/impl/http-outgoing-message.host.js';
|
|
16
|
+
export * from './adapter/http/http-server-request.js';
|
|
17
|
+
export * from './adapter/http/http-server-response.js';
|
|
12
18
|
export * from './adapter/interfaces/logger.interface.js';
|
|
13
|
-
export * from './adapter/interfaces/request.interface.js';
|
|
14
|
-
export * from './adapter/
|
|
15
|
-
export * from './
|
|
19
|
+
export * from './adapter/interfaces/request-handler.interface.js';
|
|
20
|
+
export * from './adapter/services/logger.js';
|
|
21
|
+
export * from './adapter/http/helpers/multipart-helper.js';
|
package/cjs/adapter/adapter.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OpraAdapter = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
6
|
-
const strict_typed_events_1 = require("strict-typed-events");
|
|
7
|
-
const common_1 = require("@opra/common");
|
|
8
|
-
const metadata_resource_js_1 = require("./internal/metadata.resource.js");
|
|
9
|
-
/**
|
|
10
|
-
* @class OpraAdapter
|
|
11
|
-
*/
|
|
12
|
-
class OpraAdapter extends strict_typed_events_1.AsyncEventEmitter {
|
|
13
|
-
constructor(api) {
|
|
14
|
-
super();
|
|
15
|
-
this.api = api;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Initializes the adapter
|
|
19
|
-
* @param options
|
|
20
|
-
* @protected
|
|
21
|
-
*/
|
|
22
|
-
async init(options) {
|
|
23
|
-
this.logger = options?.logger;
|
|
24
|
-
if (options?.i18n instanceof common_1.I18n)
|
|
25
|
-
this.i18n = options.i18n;
|
|
26
|
-
else if (typeof options?.i18n === 'function')
|
|
27
|
-
this.i18n = await options.i18n();
|
|
28
|
-
else
|
|
29
|
-
this.i18n = await this._createI18n(options?.i18n);
|
|
30
|
-
this.i18n = this.i18n || common_1.I18n.defaultInstance;
|
|
31
|
-
if (!this.i18n.isInitialized)
|
|
32
|
-
await this.i18n.init();
|
|
33
|
-
if (options?.onRequest)
|
|
34
|
-
this.on('request', options.onRequest);
|
|
35
|
-
this._apiRoot = await common_1.DocumentFactory.createDocument({
|
|
36
|
-
version: common_1.OpraSchema.SpecVersion,
|
|
37
|
-
info: {
|
|
38
|
-
version: common_1.OpraSchema.SpecVersion,
|
|
39
|
-
title: 'Internal resources',
|
|
40
|
-
},
|
|
41
|
-
references: { 'api': this.api },
|
|
42
|
-
resources: [new metadata_resource_js_1.MetadataResource(this.api)]
|
|
43
|
-
});
|
|
44
|
-
const promises = [];
|
|
45
|
-
for (const r of this.api.resources.values()) {
|
|
46
|
-
const onInit = r.onInit;
|
|
47
|
-
if (onInit)
|
|
48
|
-
promises.push((async () => onInit.call(r.controller, r))());
|
|
49
|
-
}
|
|
50
|
-
await Promise.all(promises);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Calls shutDown hook for all resources
|
|
54
|
-
*/
|
|
55
|
-
async close() {
|
|
56
|
-
const promises = [];
|
|
57
|
-
for (const r of this.api.resources.values()) {
|
|
58
|
-
const onShutdown = r.onShutdown;
|
|
59
|
-
if (onShutdown)
|
|
60
|
-
promises.push((async () => onShutdown.call(r.controller, r))());
|
|
61
|
-
}
|
|
62
|
-
await Promise.allSettled(promises);
|
|
63
|
-
}
|
|
64
|
-
async executeRequest(context) {
|
|
65
|
-
await this.emitAsync('request', context);
|
|
66
|
-
const { request, response } = context;
|
|
67
|
-
const { resource, operation } = request;
|
|
68
|
-
if (resource instanceof common_1.Collection || resource instanceof common_1.Singleton) {
|
|
69
|
-
const endpoint = resource.operations[operation];
|
|
70
|
-
if (!endpoint?.handler)
|
|
71
|
-
throw new common_1.ForbiddenError({
|
|
72
|
-
message: (0, common_1.translate)('RESOLVER_FORBIDDEN', { operation }, `The resource endpoint does not accept '{{operation}}' operations`),
|
|
73
|
-
severity: 'error',
|
|
74
|
-
code: 'RESOLVER_FORBIDDEN'
|
|
75
|
-
});
|
|
76
|
-
const value = await endpoint.handler(context);
|
|
77
|
-
// if (value == null)
|
|
78
|
-
if (value != null)
|
|
79
|
-
response.value = value;
|
|
80
|
-
const { crud, many } = request;
|
|
81
|
-
if (crud === 'delete' || (crud === 'update' && many)) {
|
|
82
|
-
let affected = 0;
|
|
83
|
-
if (typeof response.value === 'number')
|
|
84
|
-
affected = response.value;
|
|
85
|
-
if (typeof response.value === 'boolean')
|
|
86
|
-
affected = response.value ? 1 : 0;
|
|
87
|
-
if (typeof response.value === 'object')
|
|
88
|
-
affected = response.value.affectedRows || response.value.affected;
|
|
89
|
-
response.value = {
|
|
90
|
-
operation: request.crud,
|
|
91
|
-
affected
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
else if (response.value != null) {
|
|
95
|
-
if (!request.many)
|
|
96
|
-
response.value = Array.isArray(response.value) ? response.value[0] : response.value;
|
|
97
|
-
else
|
|
98
|
-
response.value = Array.isArray(response.value) ? response.value : [response.value];
|
|
99
|
-
}
|
|
100
|
-
if ((request.operation === 'get' || request.operation === 'update') && response.value == null)
|
|
101
|
-
throw new common_1.ResourceNotFoundError(resource.name, request.args.key);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async _createI18n(options) {
|
|
105
|
-
const opts = {
|
|
106
|
-
...options,
|
|
107
|
-
};
|
|
108
|
-
delete opts.resourceDirs;
|
|
109
|
-
const instance = common_1.I18n.createInstance(opts);
|
|
110
|
-
await instance.init();
|
|
111
|
-
await instance.loadResourceDir(path_1.default.resolve((0, common_1.getStackFileName)(), '../../../i18n'));
|
|
112
|
-
if (options?.resourceDirs)
|
|
113
|
-
for (const dir of options.resourceDirs)
|
|
114
|
-
await instance.loadResourceDir(dir);
|
|
115
|
-
return instance;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
exports.OpraAdapter = OpraAdapter;
|