@furystack/rest-service 7.0.0 → 7.0.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/esm/actions/error-action.js +25 -0
- package/esm/actions/error-action.js.map +1 -0
- package/esm/actions/get-current-user.js +12 -0
- package/esm/actions/get-current-user.js.map +1 -0
- package/esm/actions/index.js +7 -0
- package/esm/actions/index.js.map +1 -0
- package/esm/actions/is-authenticated.js +13 -0
- package/esm/actions/is-authenticated.js.map +1 -0
- package/esm/actions/login.js +22 -0
- package/esm/actions/login.js.map +1 -0
- package/esm/actions/logout.js +15 -0
- package/esm/actions/logout.js.map +1 -0
- package/esm/actions/not-found-action.js +10 -0
- package/esm/actions/not-found-action.js.map +1 -0
- package/esm/api-manager.js +153 -0
- package/esm/api-manager.js.map +1 -0
- package/esm/authenticate.js +16 -0
- package/esm/authenticate.js.map +1 -0
- package/esm/authorize.js +19 -0
- package/esm/authorize.js.map +1 -0
- package/esm/endpoint-generators/create-delete-endpoint.js +20 -0
- package/esm/endpoint-generators/create-delete-endpoint.js.map +1 -0
- package/esm/endpoint-generators/create-get-collection-endpoint.js +22 -0
- package/esm/endpoint-generators/create-get-collection-endpoint.js.map +1 -0
- package/esm/endpoint-generators/create-get-entity-endpoint.js +25 -0
- package/esm/endpoint-generators/create-get-entity-endpoint.js.map +1 -0
- package/esm/endpoint-generators/create-patch-endpoint.js +22 -0
- package/esm/endpoint-generators/create-patch-endpoint.js.map +1 -0
- package/esm/endpoint-generators/create-post-endpoint.js +25 -0
- package/esm/endpoint-generators/create-post-endpoint.js.map +1 -0
- package/esm/endpoint-generators/index.js +6 -0
- package/esm/endpoint-generators/index.js.map +1 -0
- package/esm/endpoint-generators/utils.js +23 -0
- package/esm/endpoint-generators/utils.js.map +1 -0
- package/esm/helpers.js +28 -0
- package/esm/helpers.js.map +1 -0
- package/esm/http-authentication-settings.js +26 -0
- package/esm/http-authentication-settings.js.map +1 -0
- package/esm/http-user-context.js +168 -0
- package/esm/http-user-context.js.map +1 -0
- package/esm/incoming-message-extensions.js +11 -0
- package/esm/incoming-message-extensions.js.map +1 -0
- package/esm/index.js +19 -0
- package/esm/index.js.map +1 -0
- package/esm/mime-types.js +333 -0
- package/esm/mime-types.js.map +1 -0
- package/esm/models/cors-options.js +2 -0
- package/esm/models/cors-options.js.map +1 -0
- package/esm/models/default-session.js +6 -0
- package/esm/models/default-session.js.map +1 -0
- package/esm/models/index.js +3 -0
- package/esm/models/index.js.map +1 -0
- package/esm/request-action-implementation.js +34 -0
- package/esm/request-action-implementation.js.map +1 -0
- package/esm/schema-validator/index.js +3 -0
- package/esm/schema-validator/index.js.map +1 -0
- package/esm/schema-validator/schema-validation-error.js +11 -0
- package/esm/schema-validator/schema-validation-error.js.map +1 -0
- package/esm/schema-validator/schema-validator.js +31 -0
- package/esm/schema-validator/schema-validator.js.map +1 -0
- package/esm/schema-validator/validate-examples.js +26 -0
- package/esm/schema-validator/validate-examples.js.map +1 -0
- package/esm/server-manager.js +73 -0
- package/esm/server-manager.js.map +1 -0
- package/esm/server-response-extensions.js +12 -0
- package/esm/server-response-extensions.js.map +1 -0
- package/esm/static-server-manager.js +68 -0
- package/esm/static-server-manager.js.map +1 -0
- package/esm/utils.js +70 -0
- package/esm/utils.js.map +1 -0
- package/esm/validate.integration.schema.js +2 -0
- package/esm/validate.integration.schema.js.map +1 -0
- package/esm/validate.integration.spec.schema.json +903 -0
- package/esm/validate.js +43 -0
- package/esm/validate.js.map +1 -0
- package/package.json +9 -9
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RequestError } from '@furystack/rest';
|
|
2
|
+
import { AuthorizationError } from '@furystack/core';
|
|
3
|
+
import { JsonResult } from '../request-action-implementation';
|
|
4
|
+
import { SchemaValidationError } from '../schema-validator';
|
|
5
|
+
/**
|
|
6
|
+
* Action for unhandled (500) errors
|
|
7
|
+
* Returns a serialized error instance in JSON format.
|
|
8
|
+
*/
|
|
9
|
+
export const ErrorAction = async ({ getBody }) => {
|
|
10
|
+
const body = await getBody();
|
|
11
|
+
if (body instanceof SchemaValidationError) {
|
|
12
|
+
return JsonResult({ message: body.message, errors: body.errors }, 400);
|
|
13
|
+
}
|
|
14
|
+
if (body instanceof RequestError) {
|
|
15
|
+
return JsonResult({ message: body.message }, body.responseCode);
|
|
16
|
+
}
|
|
17
|
+
if (body instanceof AuthorizationError) {
|
|
18
|
+
return JsonResult({ message: body.message }, 403);
|
|
19
|
+
}
|
|
20
|
+
if (body instanceof Error) {
|
|
21
|
+
return JsonResult({ message: body.message }, 500);
|
|
22
|
+
}
|
|
23
|
+
return JsonResult({ message: 'An unexpected error happened' }, 500);
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=error-action.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-action.js","sourceRoot":"","sources":["../../src/actions/error-action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAEpD,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAA;AAE3D;;;GAGG;AAEH,MAAM,CAAC,MAAM,WAAW,GAGnB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;IACzB,MAAM,IAAI,GAAG,MAAM,OAAO,EAAE,CAAA;IAE5B,IAAI,IAAI,YAAY,qBAAqB,EAAE;QACzC,OAAO,UAAU,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,EAAE,GAAG,CAAC,CAAA;KACvE;IAED,IAAI,IAAI,YAAY,YAAY,EAAE;QAChC,OAAO,UAAU,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,IAAI,CAAC,YAAY,CAAC,CAAA;KAChE;IAED,IAAI,IAAI,YAAY,kBAAkB,EAAE;QACtC,OAAO,UAAU,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,CAAA;KAClD;IAED,IAAI,IAAI,YAAY,KAAK,EAAE;QACzB,OAAO,UAAU,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,CAAC,CAAA;KAClD;IAED,OAAO,UAAU,CAAC,EAAE,OAAO,EAAE,8BAA8B,EAAE,EAAE,GAAG,CAAC,CAAA;AACrE,CAAC,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getCurrentUser } from '@furystack/core';
|
|
2
|
+
import { Authenticate } from '../authenticate';
|
|
3
|
+
import { JsonResult } from '../request-action-implementation';
|
|
4
|
+
/**
|
|
5
|
+
* Action that returns the current authenticated user
|
|
6
|
+
* @param injector The injector from the current stack
|
|
7
|
+
*/
|
|
8
|
+
export const GetCurrentUser = Authenticate()(async ({ injector }) => {
|
|
9
|
+
const user = await getCurrentUser(injector);
|
|
10
|
+
return JsonResult(user);
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=get-current-user.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-current-user.js","sourceRoot":"","sources":["../../src/actions/get-current-user.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAE7D;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAEtB,YAAY,EAAE,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE;IACzC,MAAM,IAAI,GAAG,MAAM,cAAc,CAAC,QAAQ,CAAC,CAAA;IAC3C,OAAO,UAAU,CAAC,IAAI,CAAC,CAAA;AACzB,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/actions/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAA;AACxB,cAAc,oBAAoB,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isAuthenticated } from '@furystack/core';
|
|
2
|
+
import { JsonResult } from '../request-action-implementation';
|
|
3
|
+
/**
|
|
4
|
+
* Action that returns if the current user is authenticated
|
|
5
|
+
* @param options The options for the Custom Action
|
|
6
|
+
* @param options.injector The Injector from the current context
|
|
7
|
+
* @returns A standard authentication result
|
|
8
|
+
*/
|
|
9
|
+
export const IsAuthenticated = async ({ injector }) => {
|
|
10
|
+
const isAuthenticatedResult = await isAuthenticated(injector);
|
|
11
|
+
return JsonResult({ isAuthenticated: isAuthenticatedResult });
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=is-authenticated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-authenticated.js","sourceRoot":"","sources":["../../src/actions/is-authenticated.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAE7D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,eAAe,GAA4D,KAAK,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC7G,MAAM,qBAAqB,GAAG,MAAM,eAAe,CAAC,QAAQ,CAAC,CAAA;IAC7D,OAAO,UAAU,CAAC,EAAE,eAAe,EAAE,qBAAqB,EAAE,CAAC,CAAA;AAC/D,CAAC,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { HttpUserContext } from '../http-user-context';
|
|
2
|
+
import '../helpers';
|
|
3
|
+
import { RequestError } from '@furystack/rest';
|
|
4
|
+
import { JsonResult } from '../request-action-implementation';
|
|
5
|
+
/**
|
|
6
|
+
* Action that logs in the current user
|
|
7
|
+
* Should be called with a JSON Post body with ``username`` and ``password`` fields.
|
|
8
|
+
* Returns the current user instance
|
|
9
|
+
*/
|
|
10
|
+
export const LoginAction = async ({ injector, getBody, response }) => {
|
|
11
|
+
const userContext = injector.getInstance(HttpUserContext);
|
|
12
|
+
const body = await getBody();
|
|
13
|
+
try {
|
|
14
|
+
const user = await userContext.authenticateUser(body.username, body.password);
|
|
15
|
+
await userContext.cookieLogin(user, response);
|
|
16
|
+
return JsonResult(user, 200);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
throw new RequestError('Login Failed', 400);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=login.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login.js","sourceRoot":"","sources":["../../src/actions/login.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,YAAY,CAAA;AAEnB,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAE7D;;;;GAIG;AAEH,MAAM,CAAC,MAAM,WAAW,GAGnB,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC7C,MAAM,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,eAAe,CAAC,CAAA;IACzD,MAAM,IAAI,GAAG,MAAM,OAAO,EAAE,CAAA;IAC5B,IAAI;QACF,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,gBAAgB,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;QAC7E,MAAM,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;QAC7C,OAAO,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;KAC7B;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,IAAI,YAAY,CAAC,cAAc,EAAE,GAAG,CAAC,CAAA;KAC5C;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HttpUserContext } from '../http-user-context';
|
|
2
|
+
import { EmptyResult } from '../request-action-implementation';
|
|
3
|
+
/**
|
|
4
|
+
* Action that logs out the current user
|
|
5
|
+
* @param root0 The Options object
|
|
6
|
+
* @param root0.injector The injector from the context
|
|
7
|
+
* @param root0.request The current Request object
|
|
8
|
+
* @param root0.response The Response object
|
|
9
|
+
* @returns An empty result that indicates the success
|
|
10
|
+
*/
|
|
11
|
+
export const LogoutAction = async ({ injector, request, response }) => {
|
|
12
|
+
await injector.getInstance(HttpUserContext).cookieLogout(request, response);
|
|
13
|
+
return EmptyResult();
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=logout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logout.js","sourceRoot":"","sources":["../../src/actions/logout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAEtD,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAA;AAE9D;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAuC,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE;IACxG,MAAM,QAAQ,CAAC,WAAW,CAAC,eAAe,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;IAC3E,OAAO,WAAW,EAAE,CAAA;AACtB,CAAC,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { JsonResult } from '../request-action-implementation';
|
|
2
|
+
/**
|
|
3
|
+
* @returns The standard Not Found action result
|
|
4
|
+
*/
|
|
5
|
+
export const NotFoundAction = async () => {
|
|
6
|
+
return JsonResult({
|
|
7
|
+
error: 'Content not found',
|
|
8
|
+
}, 404);
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=not-found-action.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"not-found-action.js","sourceRoot":"","sources":["../../src/actions/not-found-action.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAE7D;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAiD,KAAK,IAAI,EAAE;IACrF,OAAO,UAAU,CACf;QACE,KAAK,EAAE,mBAAmB;KAC3B,EACD,GAAG,CACJ,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { PathHelper, usingAsync } from '@furystack/utils';
|
|
11
|
+
import { deserializeQueryString } from '@furystack/rest';
|
|
12
|
+
import { Injectable, Injected } from '@furystack/inject';
|
|
13
|
+
import { ServerManager } from './server-manager';
|
|
14
|
+
import { match } from 'path-to-regexp';
|
|
15
|
+
import { NotFoundAction } from './actions/not-found-action';
|
|
16
|
+
import { Utils } from './utils';
|
|
17
|
+
import { ErrorAction } from './actions/error-action';
|
|
18
|
+
import './server-response-extensions';
|
|
19
|
+
import { IdentityContext } from '@furystack/core';
|
|
20
|
+
import { HttpUserContext } from './http-user-context';
|
|
21
|
+
let ApiManager = class ApiManager {
|
|
22
|
+
constructor() {
|
|
23
|
+
this.apis = new Map();
|
|
24
|
+
}
|
|
25
|
+
dispose() {
|
|
26
|
+
this.apis.clear();
|
|
27
|
+
}
|
|
28
|
+
getSuportedMethods(api) {
|
|
29
|
+
return Object.keys(api);
|
|
30
|
+
}
|
|
31
|
+
compileApi(api, root) {
|
|
32
|
+
const supportedMethods = this.getSuportedMethods(api);
|
|
33
|
+
const compiledApi = {};
|
|
34
|
+
supportedMethods.forEach((method) => {
|
|
35
|
+
compiledApi[method] = [
|
|
36
|
+
...Object.entries(api[method]).map(([path, action]) => {
|
|
37
|
+
const fullPath = `/${PathHelper.normalize(PathHelper.joinPaths(root, path))}`;
|
|
38
|
+
const matcher = match(fullPath, { decode: decodeURIComponent });
|
|
39
|
+
return { method, fullPath, matcher, action: action };
|
|
40
|
+
}),
|
|
41
|
+
];
|
|
42
|
+
});
|
|
43
|
+
return compiledApi;
|
|
44
|
+
}
|
|
45
|
+
async addApi({ api, hostName, port, root, cors, injector, deserializeQueryParams, }) {
|
|
46
|
+
const supportedMethods = this.getSuportedMethods(api);
|
|
47
|
+
const rootApiPath = PathHelper.normalize(root);
|
|
48
|
+
const server = await this.serverManager.getOrCreate({ hostName, port });
|
|
49
|
+
const compiledApi = this.compileApi(api, root);
|
|
50
|
+
server.apis.push({
|
|
51
|
+
shouldExec: (msg) => this.shouldExecRequest({
|
|
52
|
+
...msg,
|
|
53
|
+
method: msg.req.method?.toUpperCase(),
|
|
54
|
+
rootApiPath,
|
|
55
|
+
supportedMethods,
|
|
56
|
+
url: PathHelper.normalize(msg.req.url || ''),
|
|
57
|
+
}),
|
|
58
|
+
onRequest: (msg) => this.onMessage({
|
|
59
|
+
...msg,
|
|
60
|
+
compiledApi,
|
|
61
|
+
rootApiPath,
|
|
62
|
+
port,
|
|
63
|
+
supportedMethods,
|
|
64
|
+
cors,
|
|
65
|
+
injector,
|
|
66
|
+
hostName,
|
|
67
|
+
deserializeQueryParams,
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
shouldExecRequest(options) {
|
|
72
|
+
return options.method &&
|
|
73
|
+
options.url &&
|
|
74
|
+
(options.supportedMethods.includes(options.method) || options.method === 'OPTIONS') &&
|
|
75
|
+
PathHelper.normalize(options.url).startsWith(options.rootApiPath)
|
|
76
|
+
? true
|
|
77
|
+
: false;
|
|
78
|
+
}
|
|
79
|
+
getActionFromEndpoint(compiledEndpoint, fullUrl, method) {
|
|
80
|
+
let resolvedParams;
|
|
81
|
+
const action = compiledEndpoint[method]?.find((route) => {
|
|
82
|
+
const result = route.matcher(fullUrl.pathname);
|
|
83
|
+
if (result) {
|
|
84
|
+
resolvedParams = result.params;
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
});
|
|
88
|
+
return (action && {
|
|
89
|
+
...action,
|
|
90
|
+
params: resolvedParams,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async executeAction({ injector, req, res, fullUrl, action, deserializeQueryParams, params, }) {
|
|
94
|
+
await usingAsync(injector.createChild(), async (i) => {
|
|
95
|
+
const utils = i.getInstance(Utils);
|
|
96
|
+
const httpUserContext = i.getInstance(HttpUserContext);
|
|
97
|
+
i.setExplicitInstance({
|
|
98
|
+
getCurrentUser: () => httpUserContext.getCurrentUser(req),
|
|
99
|
+
isAuthorized: (...roles) => httpUserContext.isAuthorized(req, ...roles),
|
|
100
|
+
isAuthenticated: () => httpUserContext.isAuthenticated(req),
|
|
101
|
+
}, IdentityContext);
|
|
102
|
+
try {
|
|
103
|
+
const actionResult = await action({
|
|
104
|
+
request: req,
|
|
105
|
+
response: res,
|
|
106
|
+
injector: i,
|
|
107
|
+
getBody: () => utils.readPostBody(req),
|
|
108
|
+
headers: req.headers,
|
|
109
|
+
getQuery: () => deserializeQueryParams ? deserializeQueryParams(fullUrl.search) : deserializeQueryString(fullUrl.search),
|
|
110
|
+
getUrlParams: () => {
|
|
111
|
+
return params;
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
res.sendActionResult(actionResult);
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
const errorActionResult = await ErrorAction({
|
|
118
|
+
request: req,
|
|
119
|
+
response: res,
|
|
120
|
+
injector: i,
|
|
121
|
+
getBody: async () => error,
|
|
122
|
+
});
|
|
123
|
+
res.sendActionResult(errorActionResult);
|
|
124
|
+
}
|
|
125
|
+
return;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async onMessage(options) {
|
|
129
|
+
const fullUrl = new URL(PathHelper.joinPaths('http://', `${options.hostName || ServerManager.DEFAULT_HOST}:${options.port}`, options.req.url));
|
|
130
|
+
options.cors && options.injector.getInstance(Utils).addCorsHeaders(options.cors, options.req, options.res);
|
|
131
|
+
if (options.req.method === 'OPTIONS') {
|
|
132
|
+
options.res.writeHead(200);
|
|
133
|
+
options.res.end();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const action = this.getActionFromEndpoint(options.compiledApi, fullUrl, options.req.method?.toUpperCase());
|
|
137
|
+
if (action) {
|
|
138
|
+
await this.executeAction({ ...options, ...action, fullUrl });
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
options.res.sendActionResult(await NotFoundAction({ injector: options.injector, request: options.req, response: options.res }));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
__decorate([
|
|
146
|
+
Injected(ServerManager),
|
|
147
|
+
__metadata("design:type", ServerManager)
|
|
148
|
+
], ApiManager.prototype, "serverManager", void 0);
|
|
149
|
+
ApiManager = __decorate([
|
|
150
|
+
Injectable({ lifetime: 'singleton' })
|
|
151
|
+
], ApiManager);
|
|
152
|
+
export { ApiManager };
|
|
153
|
+
//# sourceMappingURL=api-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-manager.js","sourceRoot":"","sources":["../src/api-manager.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAEzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAA;AAExD,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAExD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAEhD,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAE3D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AACpD,OAAO,8BAA8B,CAAA;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AA0CrD,IAAa,UAAU,GAAvB,MAAa,UAAU;IAAvB;QACmB,SAAI,GAAG,IAAI,GAAG,EAA0B,CAAA;IA6K3D,CAAC;IA3KQ,OAAO;QACZ,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAA;IACnB,CAAC;IAEO,kBAAkB,CAAC,GAA+B;QACxD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAa,CAAA;IACrC,CAAC;IAEO,UAAU,CAAoB,GAA6B,EAAE,IAAY;QAC/E,MAAM,gBAAgB,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAA;QAErD,MAAM,WAAW,GAAmB,EAAE,CAAA;QACtC,gBAAgB,CAAC,OAAO,CAAC,CAAC,MAAc,EAAE,EAAE;YAC1C,WAAW,CAAC,MAAM,CAAC,GAAG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AG,EAAE,QAAQ,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAClG,CAAA;SACF;IACH,CAAC;CAIF,CAAA;AADkB;IADhB,QAAQ,CAAC,aAAa,CAAC;8BACS,aAAa;iDAAA;AA7KnC,UAAU;IADtB,UAAU,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;GACzB,UAAU,CA8KtB;SA9KY,UAAU"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isAuthenticated } from '@furystack/core';
|
|
2
|
+
import { sleepAsync } from '@furystack/utils';
|
|
3
|
+
import { HttpUserContext } from './http-user-context';
|
|
4
|
+
import { JsonResult } from './request-action-implementation';
|
|
5
|
+
export const Authenticate = () => (action) => {
|
|
6
|
+
return async (args) => {
|
|
7
|
+
const { injector } = args;
|
|
8
|
+
const authenticated = await isAuthenticated(injector);
|
|
9
|
+
if (!authenticated) {
|
|
10
|
+
await sleepAsync(Math.random() * 1000);
|
|
11
|
+
return JsonResult({ error: 'unauthorized' }, 401, injector.getInstance(HttpUserContext).authentication.enableBasicAuth ? { 'WWW-Authenticate': 'Basic' } : {});
|
|
12
|
+
}
|
|
13
|
+
return (await action(args));
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=authenticate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authenticate.js","sourceRoot":"","sources":["../src/authenticate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAErD,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAE5D,MAAM,CAAC,MAAM,YAAY,GACvB,GAAG,EAAE,CACL,CAAgC,MAAwB,EAAoB,EAAE;IAC5E,OAAO,KAAK,EAAE,IAA6B,EAA4B,EAAE;QACvE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAA;QACzB,MAAM,aAAa,GAAG,MAAM,eAAe,CAAC,QAAQ,CAAC,CAAA;QACrD,IAAI,CAAC,aAAa,EAAE;YAClB,MAAM,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAA;YACtC,OAAO,UAAU,CACf,EAAE,KAAK,EAAE,cAAc,EAAE,EACzB,GAAG,EACH,QAAQ,CAAC,WAAW,CAAC,eAAe,CAAC,CAAC,cAAc,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,kBAAkB,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAC9E,CAAA;SAChC;QACD,OAAO,CAAC,MAAM,MAAM,CAAC,IAAI,CAAC,CAAQ,CAAA;IACpC,CAAC,CAAA;AACH,CAAC,CAAA"}
|
package/esm/authorize.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isAuthorized } from '@furystack/core';
|
|
2
|
+
import { sleepAsync } from '@furystack/utils';
|
|
3
|
+
import { JsonResult } from './request-action-implementation';
|
|
4
|
+
export const Authorize = (...roles) => (action) => {
|
|
5
|
+
return async (options) => {
|
|
6
|
+
try {
|
|
7
|
+
const authorized = await isAuthorized(options.injector, ...roles);
|
|
8
|
+
if (!authorized) {
|
|
9
|
+
await sleepAsync(Math.random() * 1000);
|
|
10
|
+
return JsonResult({ error: 'forbidden' }, 403);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
return JsonResult({ error: 'forbidden' }, 403);
|
|
15
|
+
}
|
|
16
|
+
return (await action(options));
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=authorize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorize.js","sourceRoot":"","sources":["../src/authorize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAE5D,MAAM,CAAC,MAAM,SAAS,GACpB,CAAC,GAAG,KAAe,EAAE,EAAE,CACvB,CAAgC,MAAwB,EAAoB,EAAE;IAC5E,OAAO,KAAK,EAAE,OAAgC,EAA4B,EAAE;QAC1E,IAAI;YACF,MAAM,UAAU,GAAG,MAAM,YAAY,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,KAAK,CAAC,CAAA;YACjE,IAAI,CAAC,UAAU,EAAE;gBACf,MAAM,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAA;gBACtC,OAAO,UAAU,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,GAAG,CAAQ,CAAA;aACtD;SACF;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,UAAU,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,GAAG,CAAQ,CAAA;SACtD;QACD,OAAO,CAAC,MAAM,MAAM,CAAC,OAAO,CAAC,CAAQ,CAAA;IACvC,CAAC,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import '@furystack/repository';
|
|
2
|
+
import { JsonResult } from '../request-action-implementation';
|
|
3
|
+
import { getRepository } from '@furystack/repository';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a DELETE endpoint for removing entities
|
|
6
|
+
* @param options The options for endpoint creation
|
|
7
|
+
* @param options.model The Model class
|
|
8
|
+
* @param options.primaryKey The field used as primary key on the model
|
|
9
|
+
* @returns a boolean that indicates the success
|
|
10
|
+
*/
|
|
11
|
+
export const createDeleteEndpoint = (options) => {
|
|
12
|
+
const endpoint = async ({ injector, getUrlParams }) => {
|
|
13
|
+
const { id } = getUrlParams();
|
|
14
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey);
|
|
15
|
+
await dataSet.remove(injector, id);
|
|
16
|
+
return JsonResult({}, 204);
|
|
17
|
+
};
|
|
18
|
+
return endpoint;
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=create-delete-endpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-delete-endpoint.js","sourceRoot":"","sources":["../../src/endpoint-generators/create-delete-endpoint.ts"],"names":[],"mappings":"AAEA,OAAO,uBAAuB,CAAA;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAErD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAgD,OAGnF,EAAE,EAAE;IACH,MAAM,QAAQ,GAAkD,KAAK,EAAE,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE;QACnG,MAAM,EAAE,EAAE,EAAE,GAAG,YAAY,EAAE,CAAA;QAC7B,MAAM,OAAO,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAA;QACxF,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;QAClC,OAAO,UAAU,CAAC,EAAE,EAAE,GAAG,CAAC,CAAA;IAC5B,CAAC,CAAA;IACD,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import '@furystack/repository';
|
|
2
|
+
import { JsonResult } from '../request-action-implementation';
|
|
3
|
+
import { getRepository } from '@furystack/repository';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a GetCollection endpoint for the given model. The model should have a Repository DataSet
|
|
6
|
+
* @param options The options for endpoint creation
|
|
7
|
+
* @param options.model The Model class
|
|
8
|
+
* @param options.primaryKey The field used as primary key on the model
|
|
9
|
+
* @returns The created endpoint
|
|
10
|
+
*/
|
|
11
|
+
export const createGetCollectionEndpoint = (options) => {
|
|
12
|
+
const endpoint = async ({ injector, getQuery }) => {
|
|
13
|
+
const { findOptions } = getQuery();
|
|
14
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey);
|
|
15
|
+
const entriesPromise = dataSet.find(injector, findOptions || {});
|
|
16
|
+
const countPromise = dataSet.count(injector, findOptions?.filter);
|
|
17
|
+
const [entries, count] = await Promise.all([entriesPromise, countPromise]);
|
|
18
|
+
return JsonResult({ entries, count });
|
|
19
|
+
};
|
|
20
|
+
return endpoint;
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=create-get-collection-endpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-get-collection-endpoint.js","sourceRoot":"","sources":["../../src/endpoint-generators/create-get-collection-endpoint.ts"],"names":[],"mappings":"AAEA,OAAO,uBAAuB,CAAA;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAErD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAAiC,OAG3E,EAAE,EAAE;IACH,MAAM,QAAQ,GAA4C,KAAK,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE;QACzF,MAAM,EAAE,WAAW,EAAE,GAAG,QAAQ,EAAE,CAAA;QAClC,MAAM,OAAO,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAA;QACxF,MAAM,cAAc,GAAG,OAAO,CAAC,IAAI,CAAM,QAAQ,EAAE,WAAW,IAAI,EAAE,CAAC,CAAA;QACrE,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,CAAC,CAAA;QACjE,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC,CAAA;QAE1E,OAAO,UAAU,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAA;IACvC,CAAC,CAAA;IACD,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RequestError } from '@furystack/rest';
|
|
2
|
+
import '@furystack/repository';
|
|
3
|
+
import { JsonResult } from '../request-action-implementation';
|
|
4
|
+
import { getRepository } from '@furystack/repository';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a simple Get Entity endpoint for a specified model.
|
|
7
|
+
* @param options The options for endpoint creation
|
|
8
|
+
* @param options.model The entity model, should have a Repository DataSet
|
|
9
|
+
* @param options.primaryKey The field name used as primary key on the model
|
|
10
|
+
* @returns The generated endpoint
|
|
11
|
+
*/
|
|
12
|
+
export const createGetEntityEndpoint = (options) => {
|
|
13
|
+
const endpoint = async ({ injector, getUrlParams, getQuery }) => {
|
|
14
|
+
const { id } = getUrlParams();
|
|
15
|
+
const { select } = getQuery();
|
|
16
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey);
|
|
17
|
+
const entry = await dataSet.get(injector, id, select);
|
|
18
|
+
if (!entry) {
|
|
19
|
+
throw new RequestError('Entity not found', 404);
|
|
20
|
+
}
|
|
21
|
+
return JsonResult(entry);
|
|
22
|
+
};
|
|
23
|
+
return endpoint;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=create-get-entity-endpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-get-entity-endpoint.js","sourceRoot":"","sources":["../../src/endpoint-generators/create-get-entity-endpoint.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,uBAAuB,CAAA;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAErD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAgD,OAGtF,EAAE,EAAE;IACH,MAAM,QAAQ,GAAqD,KAAK,EAAE,EAAE,QAAQ,EAAE,YAAY,EAAE,QAAQ,EAAE,EAAE,EAAE;QAChH,MAAM,EAAE,EAAE,EAAE,GAAG,YAAY,EAAE,CAAA;QAC7B,MAAM,EAAE,MAAM,EAAE,GAAG,QAAQ,EAAE,CAAA;QAC7B,MAAM,OAAO,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAA;QACxF,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,EAAE,MAAM,CAAC,CAAA;QACrD,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,YAAY,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAA;SAChD;QACD,OAAO,UAAU,CAAC,KAAK,CAAC,CAAA;IAC1B,CAAC,CAAA;IACD,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import '@furystack/repository';
|
|
2
|
+
import '../incoming-message-extensions';
|
|
3
|
+
import { JsonResult } from '../request-action-implementation';
|
|
4
|
+
import { getRepository } from '@furystack/repository';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a PATCH endpoint for updating entities
|
|
7
|
+
* @param options The options for endpoint creation
|
|
8
|
+
* @param options.model The Model class
|
|
9
|
+
* @param options.primaryKey The field name that is used as primary key on the model
|
|
10
|
+
* @returns a boolean that indicates the success
|
|
11
|
+
*/
|
|
12
|
+
export const createPatchEndpoint = (options) => {
|
|
13
|
+
const endpoint = async ({ injector, request, getUrlParams, }) => {
|
|
14
|
+
const { id } = getUrlParams();
|
|
15
|
+
const patchData = await request.readPostBody();
|
|
16
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey);
|
|
17
|
+
await dataSet.update(injector, id, patchData);
|
|
18
|
+
return JsonResult({});
|
|
19
|
+
};
|
|
20
|
+
return endpoint;
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=create-patch-endpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-patch-endpoint.js","sourceRoot":"","sources":["../../src/endpoint-generators/create-patch-endpoint.ts"],"names":[],"mappings":"AAEA,OAAO,uBAAuB,CAAA;AAC9B,OAAO,gCAAgC,CAAA;AAEvC,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAGrD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAIjC,OAGD,EAAE,EAAE;IACH,MAAM,QAAQ,GAAgE,KAAK,EAAE,EACnF,QAAQ,EACR,OAAO,EACP,YAAY,GACb,EAAE,EAAE;QACH,MAAM,EAAE,EAAE,EAAE,GAAG,YAAY,EAAE,CAAA;QAC7B,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,YAAY,EAAK,CAAA;QACjD,MAAM,OAAO,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAA;QACxF,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,EAAE,SAAS,CAAC,CAAA;QAC7C,OAAO,UAAU,CAAC,EAAE,CAAC,CAAA;IACvB,CAAC,CAAA;IACD,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RequestError } from '@furystack/rest';
|
|
2
|
+
import '@furystack/repository';
|
|
3
|
+
import '../incoming-message-extensions';
|
|
4
|
+
import { JsonResult } from '../request-action-implementation';
|
|
5
|
+
import { getRepository } from '@furystack/repository';
|
|
6
|
+
/**
|
|
7
|
+
* Creates a POST endpoint for updating entities
|
|
8
|
+
* @param options The options for endpoint creation
|
|
9
|
+
* @param options.model The Model class
|
|
10
|
+
* @param options.primaryKey The field name used as primary key
|
|
11
|
+
* @returns a boolean that indicates the success
|
|
12
|
+
*/
|
|
13
|
+
export const createPostEndpoint = (options) => {
|
|
14
|
+
const endpoint = async ({ injector, request }) => {
|
|
15
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey);
|
|
16
|
+
const entityToCreate = await request.readPostBody();
|
|
17
|
+
const { created } = await dataSet.add(injector, entityToCreate);
|
|
18
|
+
if (!created || !created.length) {
|
|
19
|
+
throw new RequestError('Entity not found', 404);
|
|
20
|
+
}
|
|
21
|
+
return JsonResult(created[0], 201);
|
|
22
|
+
};
|
|
23
|
+
return endpoint;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=create-post-endpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-post-endpoint.js","sourceRoot":"","sources":["../../src/endpoint-generators/create-post-endpoint.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,uBAAuB,CAAA;AAC9B,OAAO,gCAAgC,CAAA;AAEvC,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AAE7D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AACrD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAIhC,OAGD,EAAE,EAAE;IACH,MAAM,QAAQ,GAA+D,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE;QAC3G,MAAM,OAAO,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,aAAa,CACnD,OAAO,CAAC,KAAK,EACb,OAAO,CAAC,UAAU,CACnB,CAAA;QAED,MAAM,cAAc,GAAG,MAAM,OAAO,CAAC,YAAY,EAAiB,CAAA;QAClE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAA;QAC/D,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YAC/B,MAAM,IAAI,YAAY,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAA;SAChD;QACD,OAAO,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;IACpC,CAAC,CAAA;IACD,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/endpoint-generators/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAA;AACxC,cAAc,kCAAkC,CAAA;AAChD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,yBAAyB,CAAA;AACvC,cAAc,wBAAwB,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { addStore, InMemoryStore, User } from '@furystack/core';
|
|
2
|
+
import { DefaultSession } from '../models/default-session';
|
|
3
|
+
import '@furystack/repository';
|
|
4
|
+
import '../helpers';
|
|
5
|
+
import { getRepository } from '@furystack/repository';
|
|
6
|
+
export class MockClass {
|
|
7
|
+
}
|
|
8
|
+
export const setupContext = (i) => {
|
|
9
|
+
addStore(i, new InMemoryStore({
|
|
10
|
+
model: MockClass,
|
|
11
|
+
primaryKey: 'id',
|
|
12
|
+
}))
|
|
13
|
+
.addStore(new InMemoryStore({
|
|
14
|
+
model: User,
|
|
15
|
+
primaryKey: 'username',
|
|
16
|
+
}))
|
|
17
|
+
.addStore(new InMemoryStore({
|
|
18
|
+
model: DefaultSession,
|
|
19
|
+
primaryKey: 'sessionId',
|
|
20
|
+
}));
|
|
21
|
+
getRepository(i).createDataSet(MockClass, 'id');
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/endpoint-generators/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC1D,OAAO,uBAAuB,CAAA;AAC9B,OAAO,YAAY,CAAA;AACnB,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAErD,MAAM,OAAO,SAAS;CAGrB;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAW,EAAE,EAAE;IAC1C,QAAQ,CACN,CAAC,EACD,IAAI,aAAa,CAAC;QAChB,KAAK,EAAE,SAAS;QAChB,UAAU,EAAE,IAAI;KACjB,CAAC,CACH;SACE,QAAQ,CACP,IAAI,aAAa,CAAC;QAChB,KAAK,EAAE,IAAI;QACX,UAAU,EAAE,UAAU;KACvB,CAAC,CACH;SACA,QAAQ,CACP,IAAI,aAAa,CAAC;QAChB,KAAK,EAAE,cAAc;QACrB,UAAU,EAAE,WAAW;KACxB,CAAC,CACH,CAAA;IACH,aAAa,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,SAAS,EAAE,IAAI,CAAC,CAAA;AACjD,CAAC,CAAA"}
|
package/esm/helpers.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { HttpAuthenticationSettings } from './http-authentication-settings';
|
|
2
|
+
import { ApiManager } from './api-manager';
|
|
3
|
+
import { StaticServerManager } from './static-server-manager';
|
|
4
|
+
/**
|
|
5
|
+
* Sets up the @furystack/rest-service with the provided settings
|
|
6
|
+
* @param api The API implementation details
|
|
7
|
+
* @returns a promise that resolves when the API is added to the server
|
|
8
|
+
*/
|
|
9
|
+
export const useRestService = async (api) => await api.injector.getInstance(ApiManager).addApi({ ...api });
|
|
10
|
+
/**
|
|
11
|
+
* Sets up the HTTP Authentication
|
|
12
|
+
* @param injector The Injector instance
|
|
13
|
+
* @param settings Settings for HTTP Authentication
|
|
14
|
+
* @returns void
|
|
15
|
+
*/
|
|
16
|
+
export const useHttpAuthentication = (injector, settings) => injector.setExplicitInstance({ ...new HttpAuthenticationSettings(), ...settings }, HttpAuthenticationSettings);
|
|
17
|
+
/**
|
|
18
|
+
* Sets up a static file server
|
|
19
|
+
* @param options The settings for the static file server
|
|
20
|
+
* @param options.injector The Injector instance
|
|
21
|
+
* @param options.settings Settings for the static file server
|
|
22
|
+
* @returns a promise that resolves when the server is ready
|
|
23
|
+
*/
|
|
24
|
+
export const useStaticFiles = (options) => {
|
|
25
|
+
const { injector, ...settings } = options;
|
|
26
|
+
return injector.getInstance(StaticServerManager).addStaticSite(settings);
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,0BAA0B,EAAE,MAAM,gCAAgC,CAAA;AAG3E,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG1C,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAA;AAE7D;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EAAqB,GAA2B,EAAE,EAAE,CACrF,MAAM,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,CAAA;AAE/D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAAkB,EAClB,QAA+D,EAC/D,EAAE,CAAC,QAAQ,CAAC,mBAAmB,CAAC,EAAE,GAAG,IAAI,0BAA0B,EAAE,EAAE,GAAG,QAAQ,EAAE,EAAE,0BAA0B,CAAC,CAAA;AAEnH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,OAAqD,EAAE,EAAE;IACtF,MAAM,EAAE,QAAQ,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAA;IACzC,OAAO,QAAQ,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;AAC1E,CAAC,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { User } from '@furystack/core';
|
|
8
|
+
import { Injectable } from '@furystack/inject';
|
|
9
|
+
import { DefaultSession } from './models/default-session';
|
|
10
|
+
/**
|
|
11
|
+
* Authentication settings object for FuryStack HTTP Api
|
|
12
|
+
*/
|
|
13
|
+
let HttpAuthenticationSettings = class HttpAuthenticationSettings {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.model = User;
|
|
16
|
+
this.getUserStore = (sm) => sm.getStoreFor(User, 'username');
|
|
17
|
+
this.getSessionStore = (sm) => sm.getStoreFor(DefaultSession, 'sessionId');
|
|
18
|
+
this.cookieName = 'fss';
|
|
19
|
+
this.enableBasicAuth = true;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
HttpAuthenticationSettings = __decorate([
|
|
23
|
+
Injectable({ lifetime: 'singleton' })
|
|
24
|
+
], HttpAuthenticationSettings);
|
|
25
|
+
export { HttpAuthenticationSettings };
|
|
26
|
+
//# sourceMappingURL=http-authentication-settings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-authentication-settings.js","sourceRoot":"","sources":["../src/http-authentication-settings.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAEtC,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAEzD;;GAEG;AAEH,IAAa,0BAA0B,GAAvC,MAAa,0BAA0B;IAAvC;QACS,UAAK,GAAyB,IAA4B,CAAA;QAE1D,iBAAY,GAAsE,CAAC,EAAE,EAAE,EAAE,CAC9F,EAAE,CAAC,WAAW,CAAqB,IAAW,EAAE,UAAU,CAAC,CAAA;QAEtD,oBAAe,GAA4E,CAAC,EAAE,EAAE,EAAE,CACvG,EAAE,CAAC,WAAW,CAAC,cAAc,EAAE,WAAW,CAAuD,CAAA;QAE5F,eAAU,GAAG,KAAK,CAAA;QAClB,oBAAe,GAAG,IAAI,CAAA;IAC/B,CAAC;CAAA,CAAA;AAXY,0BAA0B;IADtC,UAAU,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;GACzB,0BAA0B,CAWtC;SAXY,0BAA0B"}
|