@ebertjendustries/cswrapper 1.0.17 → 1.0.18
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/dist/index.d.mts +54 -1
- package/dist/index.d.ts +54 -1
- package/dist/index.js +136 -0
- package/dist/index.mjs +135 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -795,4 +795,57 @@ declare const warnWhenTabIsClosed: (isDirty?: () => boolean) => () => void;
|
|
|
795
795
|
|
|
796
796
|
declare const useHotkey: (key: string, shift: boolean, ctrl: boolean, alt: boolean, callback: Function, node?: null) => void;
|
|
797
797
|
|
|
798
|
-
|
|
798
|
+
/**
|
|
799
|
+
* Builds a mockserver.
|
|
800
|
+
*
|
|
801
|
+
* NOTE - Important: This file CANNOT be moved to the cstestwrapper library. The reason is that this leads to vitest
|
|
802
|
+
* being imported indirectly in the application that imports cstestwrapper/MockServerBuilder. That leads to following
|
|
803
|
+
* error when running the MockServer.ts via vite-node:
|
|
804
|
+
*
|
|
805
|
+
* "npx vite-node ./mock-server/MockServer.ts
|
|
806
|
+
* file:///home/jens/prog/git/MyGameCollection/mgc-parent/mgc-frontend/node_modules/vitest/dist/chunks/utils.XdZDrNZV.js:9
|
|
807
|
+
* throw new Error(errorMsg);
|
|
808
|
+
* ^
|
|
809
|
+
*
|
|
810
|
+
* Error: Vitest failed to access its internal state.
|
|
811
|
+
*
|
|
812
|
+
* One of the following is possible:
|
|
813
|
+
* - "vitest" is imported directly without running "vitest" command
|
|
814
|
+
* - "vitest" is imported inside "globalSetup" (to fix this, use "setupFiles" instead, because "globalSetup" runs in a different context)
|
|
815
|
+
* - "vitest" is imported inside Vite / Vitest config file
|
|
816
|
+
* - Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues"
|
|
817
|
+
*/
|
|
818
|
+
declare class MockServerBuilder {
|
|
819
|
+
private port;
|
|
820
|
+
private mockData;
|
|
821
|
+
private redirectUrls;
|
|
822
|
+
private server;
|
|
823
|
+
private jsonServer;
|
|
824
|
+
constructor(port: number, requireFunc: Function);
|
|
825
|
+
addToMockData(key: string, data: any): void;
|
|
826
|
+
/**
|
|
827
|
+
* Puts mock data: Adds it to internal data structures and prepares it for mocking. It potentially updates
|
|
828
|
+
* the overall mock data object later passed into jsonServer router as well as the rewrite object later
|
|
829
|
+
* passed into jsonServer.rewriter.
|
|
830
|
+
*
|
|
831
|
+
* @param key The key of the data
|
|
832
|
+
* @param data The data itself
|
|
833
|
+
* @param urlRedirects The redirect URLs to be rewritten to the key url
|
|
834
|
+
* @param crud Whether this is a crud redirect
|
|
835
|
+
* @returns The loaded mock data object
|
|
836
|
+
*/
|
|
837
|
+
putMockData(key: string, data: any, urlRedirects: string[], crud?: boolean): void;
|
|
838
|
+
addToRedirects(key: string, urlRedirects: string[], crud: boolean): void;
|
|
839
|
+
rewritePostToGet(url: string): void;
|
|
840
|
+
static set200WithData(res: any, data: any): void;
|
|
841
|
+
static respondWithError(res: any, componentId: string, messageId: string, httpStatus: number, op: string, message: string): void;
|
|
842
|
+
mockCall(url: string, callProcessor: (req: any, res: any, next: Function) => void): void;
|
|
843
|
+
mockCRUDMulti(urls: string[], allElements: any, finder: (id: number) => any, operationDesc: string): void;
|
|
844
|
+
mockCRUD(url: string, allElements: any, finder: (id: number) => any, operationDesc: string): void;
|
|
845
|
+
start(): void;
|
|
846
|
+
private useCors;
|
|
847
|
+
private static allowCors;
|
|
848
|
+
private static postRewriteToGet;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
export { type BooleanStateIndication, CSAlert, CSBox, CSButton, CSCheckbox, CSCopyToClipboard, CSDatePicker, CSEnumMultiselect, CSEnumSegmentedControl, CSEnumSelect, CSEnumTiles, CSExternalLink, CSFormField, type CSFormProps, CSHeader, CSInput, CSLink, CSMultiselect, CSNumericInput, type CSReadOnlyFormProps, CSSegmentedControl, CSSelect, CSStatusIndicator, CSTextContent, CSTextarea, CSTimeInput, CSToggle, CSVEnumRatingView, type CSVEnumRatingViewElements, CSVNoItemsView, type CSVNoItemsViewElements, CSVPriceInputView, type CSVPriceInputViewElements, CSVSearchFilterView, type CSVSearchFilterViewElements, CSVTextPromptModalView, type CSVTextPromptModalViewElements, type CSVTextPromptModalViewProps, CSV_ENUM_RATING_VIEW_SPEC, CSV_NO_ITEMS_VIEW_SPEC, CSV_PRICE_INPUT_VIEW_SPEC, CSV_SEARCH_FILTER_VIEW_SPEC, CSV_TEXT_PROMPT_MODAL_VIEW_SPEC, type CSViewProps, ChildUtils, type CommandFunction, type ContentWithMessagesCTO, ControllerState, DateUtils, ELEMENT_VALIDITY_LEVEL_FACTORY, ElementValidityLevel, EnumFactory, FORM_FIELD_ID_SUFFIX, FieldProperties, FieldSpec, type FieldValidatorFunctionType, type FieldValidityLocalisationFunction, FixedWidth, FromPropsHelper, type IFieldSpec, type IPrice, type IRestClient, type IViewElementSpec, type IdType, type Indexable, type InputChangedFunction, LocalStorageService, LocalisationNamespace, type LocalisationVariableType, type LocalisedElements, type LocalisedEnumDetails, Localiser, Log, MESSAGE_SEVERITY_FACTORY, MessageCTO, MessageCodeTO, MessageService, MessageSeverity, type MessageWithDetailToggle, MockServerBuilder, type ModalState, type MultiPropertyChangeFunction, NO_HTTP_CODE, NSK_CONTENT, ObjectWithId, PriceUtils, type PropertyChangeFunction, QUERY_SEP, QUERY_START, type QueryKeyValueParams, QueryParamHelper, REST_CLIENT_LOGGER, RestClient, type RunFunction, RunFunctionResult, SelectHelper, TECHNICAL_CONTROLLER_ID, type UseAWSUIRefHook, type UseControllerDisplayStateHook, type UseMessagesHook, type UseTOArrayChangeHandlerHook, type UseTOChangeHandlerHook, ViewSpec, type ViewValidatorFunctionType, createRestClient, fatalRunErrorFunction, filterArrayByIndices, getEnumRatingLabel, getEnumRatingValues, indexRangeArray, isContentWithMessages, openInNewTab, singleChangeHandlerToMulti, sleep, sortArrayByIndices, useAWSUIRef, useControllerDisplayState, useHotkey, useMessages, useTOArrayChangeHandler, useTOChangeHandler, warnWhenTabIsClosed };
|
package/dist/index.d.ts
CHANGED
|
@@ -795,4 +795,57 @@ declare const warnWhenTabIsClosed: (isDirty?: () => boolean) => () => void;
|
|
|
795
795
|
|
|
796
796
|
declare const useHotkey: (key: string, shift: boolean, ctrl: boolean, alt: boolean, callback: Function, node?: null) => void;
|
|
797
797
|
|
|
798
|
-
|
|
798
|
+
/**
|
|
799
|
+
* Builds a mockserver.
|
|
800
|
+
*
|
|
801
|
+
* NOTE - Important: This file CANNOT be moved to the cstestwrapper library. The reason is that this leads to vitest
|
|
802
|
+
* being imported indirectly in the application that imports cstestwrapper/MockServerBuilder. That leads to following
|
|
803
|
+
* error when running the MockServer.ts via vite-node:
|
|
804
|
+
*
|
|
805
|
+
* "npx vite-node ./mock-server/MockServer.ts
|
|
806
|
+
* file:///home/jens/prog/git/MyGameCollection/mgc-parent/mgc-frontend/node_modules/vitest/dist/chunks/utils.XdZDrNZV.js:9
|
|
807
|
+
* throw new Error(errorMsg);
|
|
808
|
+
* ^
|
|
809
|
+
*
|
|
810
|
+
* Error: Vitest failed to access its internal state.
|
|
811
|
+
*
|
|
812
|
+
* One of the following is possible:
|
|
813
|
+
* - "vitest" is imported directly without running "vitest" command
|
|
814
|
+
* - "vitest" is imported inside "globalSetup" (to fix this, use "setupFiles" instead, because "globalSetup" runs in a different context)
|
|
815
|
+
* - "vitest" is imported inside Vite / Vitest config file
|
|
816
|
+
* - Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues"
|
|
817
|
+
*/
|
|
818
|
+
declare class MockServerBuilder {
|
|
819
|
+
private port;
|
|
820
|
+
private mockData;
|
|
821
|
+
private redirectUrls;
|
|
822
|
+
private server;
|
|
823
|
+
private jsonServer;
|
|
824
|
+
constructor(port: number, requireFunc: Function);
|
|
825
|
+
addToMockData(key: string, data: any): void;
|
|
826
|
+
/**
|
|
827
|
+
* Puts mock data: Adds it to internal data structures and prepares it for mocking. It potentially updates
|
|
828
|
+
* the overall mock data object later passed into jsonServer router as well as the rewrite object later
|
|
829
|
+
* passed into jsonServer.rewriter.
|
|
830
|
+
*
|
|
831
|
+
* @param key The key of the data
|
|
832
|
+
* @param data The data itself
|
|
833
|
+
* @param urlRedirects The redirect URLs to be rewritten to the key url
|
|
834
|
+
* @param crud Whether this is a crud redirect
|
|
835
|
+
* @returns The loaded mock data object
|
|
836
|
+
*/
|
|
837
|
+
putMockData(key: string, data: any, urlRedirects: string[], crud?: boolean): void;
|
|
838
|
+
addToRedirects(key: string, urlRedirects: string[], crud: boolean): void;
|
|
839
|
+
rewritePostToGet(url: string): void;
|
|
840
|
+
static set200WithData(res: any, data: any): void;
|
|
841
|
+
static respondWithError(res: any, componentId: string, messageId: string, httpStatus: number, op: string, message: string): void;
|
|
842
|
+
mockCall(url: string, callProcessor: (req: any, res: any, next: Function) => void): void;
|
|
843
|
+
mockCRUDMulti(urls: string[], allElements: any, finder: (id: number) => any, operationDesc: string): void;
|
|
844
|
+
mockCRUD(url: string, allElements: any, finder: (id: number) => any, operationDesc: string): void;
|
|
845
|
+
start(): void;
|
|
846
|
+
private useCors;
|
|
847
|
+
private static allowCors;
|
|
848
|
+
private static postRewriteToGet;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
export { type BooleanStateIndication, CSAlert, CSBox, CSButton, CSCheckbox, CSCopyToClipboard, CSDatePicker, CSEnumMultiselect, CSEnumSegmentedControl, CSEnumSelect, CSEnumTiles, CSExternalLink, CSFormField, type CSFormProps, CSHeader, CSInput, CSLink, CSMultiselect, CSNumericInput, type CSReadOnlyFormProps, CSSegmentedControl, CSSelect, CSStatusIndicator, CSTextContent, CSTextarea, CSTimeInput, CSToggle, CSVEnumRatingView, type CSVEnumRatingViewElements, CSVNoItemsView, type CSVNoItemsViewElements, CSVPriceInputView, type CSVPriceInputViewElements, CSVSearchFilterView, type CSVSearchFilterViewElements, CSVTextPromptModalView, type CSVTextPromptModalViewElements, type CSVTextPromptModalViewProps, CSV_ENUM_RATING_VIEW_SPEC, CSV_NO_ITEMS_VIEW_SPEC, CSV_PRICE_INPUT_VIEW_SPEC, CSV_SEARCH_FILTER_VIEW_SPEC, CSV_TEXT_PROMPT_MODAL_VIEW_SPEC, type CSViewProps, ChildUtils, type CommandFunction, type ContentWithMessagesCTO, ControllerState, DateUtils, ELEMENT_VALIDITY_LEVEL_FACTORY, ElementValidityLevel, EnumFactory, FORM_FIELD_ID_SUFFIX, FieldProperties, FieldSpec, type FieldValidatorFunctionType, type FieldValidityLocalisationFunction, FixedWidth, FromPropsHelper, type IFieldSpec, type IPrice, type IRestClient, type IViewElementSpec, type IdType, type Indexable, type InputChangedFunction, LocalStorageService, LocalisationNamespace, type LocalisationVariableType, type LocalisedElements, type LocalisedEnumDetails, Localiser, Log, MESSAGE_SEVERITY_FACTORY, MessageCTO, MessageCodeTO, MessageService, MessageSeverity, type MessageWithDetailToggle, MockServerBuilder, type ModalState, type MultiPropertyChangeFunction, NO_HTTP_CODE, NSK_CONTENT, ObjectWithId, PriceUtils, type PropertyChangeFunction, QUERY_SEP, QUERY_START, type QueryKeyValueParams, QueryParamHelper, REST_CLIENT_LOGGER, RestClient, type RunFunction, RunFunctionResult, SelectHelper, TECHNICAL_CONTROLLER_ID, type UseAWSUIRefHook, type UseControllerDisplayStateHook, type UseMessagesHook, type UseTOArrayChangeHandlerHook, type UseTOChangeHandlerHook, ViewSpec, type ViewValidatorFunctionType, createRestClient, fatalRunErrorFunction, filterArrayByIndices, getEnumRatingLabel, getEnumRatingValues, indexRangeArray, isContentWithMessages, openInNewTab, singleChangeHandlerToMulti, sleep, sortArrayByIndices, useAWSUIRef, useControllerDisplayState, useHotkey, useMessages, useTOArrayChangeHandler, useTOChangeHandler, warnWhenTabIsClosed };
|
package/dist/index.js
CHANGED
|
@@ -74,6 +74,7 @@ __export(index_exports, {
|
|
|
74
74
|
MessageCodeTO: () => MessageCodeTO,
|
|
75
75
|
MessageService: () => MessageService,
|
|
76
76
|
MessageSeverity: () => MessageSeverity,
|
|
77
|
+
MockServerBuilder: () => MockServerBuilder,
|
|
77
78
|
NO_HTTP_CODE: () => NO_HTTP_CODE,
|
|
78
79
|
NSK_CONTENT: () => NSK_CONTENT,
|
|
79
80
|
ObjectWithId: () => ObjectWithId,
|
|
@@ -2084,6 +2085,140 @@ function useMessages(LOGGER3) {
|
|
|
2084
2085
|
clearMessages
|
|
2085
2086
|
};
|
|
2086
2087
|
}
|
|
2088
|
+
|
|
2089
|
+
// src/utils/MockServerBuilder.ts
|
|
2090
|
+
var MockServerBuilder = class _MockServerBuilder {
|
|
2091
|
+
constructor(port, requireFunc) {
|
|
2092
|
+
this.port = port;
|
|
2093
|
+
this.mockData = {};
|
|
2094
|
+
this.redirectUrls = {};
|
|
2095
|
+
this.jsonServer = requireFunc("json-server");
|
|
2096
|
+
this.server = this.jsonServer.create();
|
|
2097
|
+
this.server.use(this.jsonServer.bodyParser);
|
|
2098
|
+
}
|
|
2099
|
+
addToMockData(key, data) {
|
|
2100
|
+
this.mockData[key] = data;
|
|
2101
|
+
}
|
|
2102
|
+
/**
|
|
2103
|
+
* Puts mock data: Adds it to internal data structures and prepares it for mocking. It potentially updates
|
|
2104
|
+
* the overall mock data object later passed into jsonServer router as well as the rewrite object later
|
|
2105
|
+
* passed into jsonServer.rewriter.
|
|
2106
|
+
*
|
|
2107
|
+
* @param key The key of the data
|
|
2108
|
+
* @param data The data itself
|
|
2109
|
+
* @param urlRedirects The redirect URLs to be rewritten to the key url
|
|
2110
|
+
* @param crud Whether this is a crud redirect
|
|
2111
|
+
* @returns The loaded mock data object
|
|
2112
|
+
*/
|
|
2113
|
+
putMockData(key, data, urlRedirects, crud) {
|
|
2114
|
+
this.addToMockData(key, data);
|
|
2115
|
+
if (crud !== void 0) {
|
|
2116
|
+
this.addToRedirects(key, urlRedirects, crud);
|
|
2117
|
+
} else {
|
|
2118
|
+
this.addToRedirects(key, urlRedirects, false);
|
|
2119
|
+
}
|
|
2120
|
+
}
|
|
2121
|
+
addToRedirects(key, urlRedirects, crud) {
|
|
2122
|
+
urlRedirects.forEach((urlRedirect) => this.redirectUrls[urlRedirect] = "/" + key);
|
|
2123
|
+
if (crud) {
|
|
2124
|
+
urlRedirects.forEach((urlRedirect) => this.redirectUrls[urlRedirect + "/:id"] = "/" + key + "/:id");
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
rewritePostToGet(url) {
|
|
2128
|
+
this.useCors(url);
|
|
2129
|
+
this.server.use(url, _MockServerBuilder.postRewriteToGet);
|
|
2130
|
+
}
|
|
2131
|
+
static set200WithData(res, data) {
|
|
2132
|
+
res.status(200).jsonp({ content: data });
|
|
2133
|
+
}
|
|
2134
|
+
static respondWithError(res, componentId, messageId, httpStatus, op, message) {
|
|
2135
|
+
res.status(httpStatus).jsonp({
|
|
2136
|
+
messages: [
|
|
2137
|
+
{
|
|
2138
|
+
"code": {
|
|
2139
|
+
"componentId": componentId,
|
|
2140
|
+
"httpStatusCode": httpStatus,
|
|
2141
|
+
"localCode": messageId,
|
|
2142
|
+
"messageSeverity": "ERR",
|
|
2143
|
+
"uniqueCode": componentId + messageId
|
|
2144
|
+
},
|
|
2145
|
+
"correlationId": "N/A",
|
|
2146
|
+
"detailMessage": message,
|
|
2147
|
+
"operation": op,
|
|
2148
|
+
"timestamp": "2022-05-10T15:17:08Z"
|
|
2149
|
+
}
|
|
2150
|
+
]
|
|
2151
|
+
});
|
|
2152
|
+
}
|
|
2153
|
+
mockCall(url, callProcessor) {
|
|
2154
|
+
this.useCors(url);
|
|
2155
|
+
this.server.use(url, callProcessor);
|
|
2156
|
+
}
|
|
2157
|
+
mockCRUDMulti(urls, allElements, finder, operationDesc) {
|
|
2158
|
+
urls.forEach((url) => this.mockCRUD(url, allElements, finder, operationDesc));
|
|
2159
|
+
}
|
|
2160
|
+
mockCRUD(url, allElements, finder, operationDesc) {
|
|
2161
|
+
this.mockCall(`${url}${url.endsWith("/") ? "" : "/"}:id`, async (req, res, next) => {
|
|
2162
|
+
const method = req.method;
|
|
2163
|
+
const id = parseInt(req.params["id"]);
|
|
2164
|
+
console.log(`Calling ${operationDesc} CRUD ${method} with id ${id}`);
|
|
2165
|
+
if (method === "GET") {
|
|
2166
|
+
let returnedEntity = finder(id);
|
|
2167
|
+
_MockServerBuilder.set200WithData(res, returnedEntity);
|
|
2168
|
+
} else {
|
|
2169
|
+
_MockServerBuilder.set200WithData(res, {});
|
|
2170
|
+
}
|
|
2171
|
+
});
|
|
2172
|
+
this.mockCall(`${url}`, async (req, res, next) => {
|
|
2173
|
+
const method = req.method;
|
|
2174
|
+
console.log(`Calling ${operationDesc} CRUD ${method}${method === "GET" ? " ALL" : ""}`);
|
|
2175
|
+
if (method === "GET") {
|
|
2176
|
+
_MockServerBuilder.set200WithData(res, allElements);
|
|
2177
|
+
} else if (method === "PUT") {
|
|
2178
|
+
_MockServerBuilder.set200WithData(res, req.body);
|
|
2179
|
+
} else {
|
|
2180
|
+
_MockServerBuilder.set200WithData(res, {});
|
|
2181
|
+
}
|
|
2182
|
+
});
|
|
2183
|
+
}
|
|
2184
|
+
start() {
|
|
2185
|
+
console.log("Mock data keys: " + Object.keys(this.mockData));
|
|
2186
|
+
console.log("Redirect URLs: ", this.redirectUrls);
|
|
2187
|
+
const router = this.jsonServer.router(this.mockData);
|
|
2188
|
+
const middlewares = this.jsonServer.defaults();
|
|
2189
|
+
this.server.use(this.jsonServer.rewriter(this.redirectUrls));
|
|
2190
|
+
Object.keys(this.redirectUrls).forEach((redirectUrl) => {
|
|
2191
|
+
this.useCors(redirectUrl);
|
|
2192
|
+
});
|
|
2193
|
+
this.server.use(middlewares);
|
|
2194
|
+
this.server.use(router);
|
|
2195
|
+
router.render = function(req, res) {
|
|
2196
|
+
res.jsonp({
|
|
2197
|
+
content: res.locals.data
|
|
2198
|
+
});
|
|
2199
|
+
};
|
|
2200
|
+
this.server.listen(this.port, () => {
|
|
2201
|
+
console.log("JSON Server is running on port " + this.port);
|
|
2202
|
+
});
|
|
2203
|
+
}
|
|
2204
|
+
useCors(url) {
|
|
2205
|
+
console.log("Configuring CORS for: ", url);
|
|
2206
|
+
this.server.use(url, _MockServerBuilder.allowCors);
|
|
2207
|
+
}
|
|
2208
|
+
static allowCors(req, res, next) {
|
|
2209
|
+
res.header("Access-Control-Allow-Origin", "*");
|
|
2210
|
+
res.header("Access-Control-Allow-Headers", "*");
|
|
2211
|
+
res.header("Access-Control-Allow-Methods", "*");
|
|
2212
|
+
next();
|
|
2213
|
+
}
|
|
2214
|
+
static postRewriteToGet(req, res, next) {
|
|
2215
|
+
if (req.method === "POST") {
|
|
2216
|
+
console.log(`Received POST request, rewriting to GET: ` + req.url);
|
|
2217
|
+
req.method = "GET";
|
|
2218
|
+
}
|
|
2219
|
+
next();
|
|
2220
|
+
}
|
|
2221
|
+
};
|
|
2087
2222
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2088
2223
|
0 && (module.exports = {
|
|
2089
2224
|
CSAlert,
|
|
@@ -2140,6 +2275,7 @@ function useMessages(LOGGER3) {
|
|
|
2140
2275
|
MessageCodeTO,
|
|
2141
2276
|
MessageService,
|
|
2142
2277
|
MessageSeverity,
|
|
2278
|
+
MockServerBuilder,
|
|
2143
2279
|
NO_HTTP_CODE,
|
|
2144
2280
|
NSK_CONTENT,
|
|
2145
2281
|
ObjectWithId,
|
package/dist/index.mjs
CHANGED
|
@@ -1974,6 +1974,140 @@ function useMessages(LOGGER3) {
|
|
|
1974
1974
|
clearMessages
|
|
1975
1975
|
};
|
|
1976
1976
|
}
|
|
1977
|
+
|
|
1978
|
+
// src/utils/MockServerBuilder.ts
|
|
1979
|
+
var MockServerBuilder = class _MockServerBuilder {
|
|
1980
|
+
constructor(port, requireFunc) {
|
|
1981
|
+
this.port = port;
|
|
1982
|
+
this.mockData = {};
|
|
1983
|
+
this.redirectUrls = {};
|
|
1984
|
+
this.jsonServer = requireFunc("json-server");
|
|
1985
|
+
this.server = this.jsonServer.create();
|
|
1986
|
+
this.server.use(this.jsonServer.bodyParser);
|
|
1987
|
+
}
|
|
1988
|
+
addToMockData(key, data) {
|
|
1989
|
+
this.mockData[key] = data;
|
|
1990
|
+
}
|
|
1991
|
+
/**
|
|
1992
|
+
* Puts mock data: Adds it to internal data structures and prepares it for mocking. It potentially updates
|
|
1993
|
+
* the overall mock data object later passed into jsonServer router as well as the rewrite object later
|
|
1994
|
+
* passed into jsonServer.rewriter.
|
|
1995
|
+
*
|
|
1996
|
+
* @param key The key of the data
|
|
1997
|
+
* @param data The data itself
|
|
1998
|
+
* @param urlRedirects The redirect URLs to be rewritten to the key url
|
|
1999
|
+
* @param crud Whether this is a crud redirect
|
|
2000
|
+
* @returns The loaded mock data object
|
|
2001
|
+
*/
|
|
2002
|
+
putMockData(key, data, urlRedirects, crud) {
|
|
2003
|
+
this.addToMockData(key, data);
|
|
2004
|
+
if (crud !== void 0) {
|
|
2005
|
+
this.addToRedirects(key, urlRedirects, crud);
|
|
2006
|
+
} else {
|
|
2007
|
+
this.addToRedirects(key, urlRedirects, false);
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
addToRedirects(key, urlRedirects, crud) {
|
|
2011
|
+
urlRedirects.forEach((urlRedirect) => this.redirectUrls[urlRedirect] = "/" + key);
|
|
2012
|
+
if (crud) {
|
|
2013
|
+
urlRedirects.forEach((urlRedirect) => this.redirectUrls[urlRedirect + "/:id"] = "/" + key + "/:id");
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
rewritePostToGet(url) {
|
|
2017
|
+
this.useCors(url);
|
|
2018
|
+
this.server.use(url, _MockServerBuilder.postRewriteToGet);
|
|
2019
|
+
}
|
|
2020
|
+
static set200WithData(res, data) {
|
|
2021
|
+
res.status(200).jsonp({ content: data });
|
|
2022
|
+
}
|
|
2023
|
+
static respondWithError(res, componentId, messageId, httpStatus, op, message) {
|
|
2024
|
+
res.status(httpStatus).jsonp({
|
|
2025
|
+
messages: [
|
|
2026
|
+
{
|
|
2027
|
+
"code": {
|
|
2028
|
+
"componentId": componentId,
|
|
2029
|
+
"httpStatusCode": httpStatus,
|
|
2030
|
+
"localCode": messageId,
|
|
2031
|
+
"messageSeverity": "ERR",
|
|
2032
|
+
"uniqueCode": componentId + messageId
|
|
2033
|
+
},
|
|
2034
|
+
"correlationId": "N/A",
|
|
2035
|
+
"detailMessage": message,
|
|
2036
|
+
"operation": op,
|
|
2037
|
+
"timestamp": "2022-05-10T15:17:08Z"
|
|
2038
|
+
}
|
|
2039
|
+
]
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
mockCall(url, callProcessor) {
|
|
2043
|
+
this.useCors(url);
|
|
2044
|
+
this.server.use(url, callProcessor);
|
|
2045
|
+
}
|
|
2046
|
+
mockCRUDMulti(urls, allElements, finder, operationDesc) {
|
|
2047
|
+
urls.forEach((url) => this.mockCRUD(url, allElements, finder, operationDesc));
|
|
2048
|
+
}
|
|
2049
|
+
mockCRUD(url, allElements, finder, operationDesc) {
|
|
2050
|
+
this.mockCall(`${url}${url.endsWith("/") ? "" : "/"}:id`, async (req, res, next) => {
|
|
2051
|
+
const method = req.method;
|
|
2052
|
+
const id = parseInt(req.params["id"]);
|
|
2053
|
+
console.log(`Calling ${operationDesc} CRUD ${method} with id ${id}`);
|
|
2054
|
+
if (method === "GET") {
|
|
2055
|
+
let returnedEntity = finder(id);
|
|
2056
|
+
_MockServerBuilder.set200WithData(res, returnedEntity);
|
|
2057
|
+
} else {
|
|
2058
|
+
_MockServerBuilder.set200WithData(res, {});
|
|
2059
|
+
}
|
|
2060
|
+
});
|
|
2061
|
+
this.mockCall(`${url}`, async (req, res, next) => {
|
|
2062
|
+
const method = req.method;
|
|
2063
|
+
console.log(`Calling ${operationDesc} CRUD ${method}${method === "GET" ? " ALL" : ""}`);
|
|
2064
|
+
if (method === "GET") {
|
|
2065
|
+
_MockServerBuilder.set200WithData(res, allElements);
|
|
2066
|
+
} else if (method === "PUT") {
|
|
2067
|
+
_MockServerBuilder.set200WithData(res, req.body);
|
|
2068
|
+
} else {
|
|
2069
|
+
_MockServerBuilder.set200WithData(res, {});
|
|
2070
|
+
}
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
start() {
|
|
2074
|
+
console.log("Mock data keys: " + Object.keys(this.mockData));
|
|
2075
|
+
console.log("Redirect URLs: ", this.redirectUrls);
|
|
2076
|
+
const router = this.jsonServer.router(this.mockData);
|
|
2077
|
+
const middlewares = this.jsonServer.defaults();
|
|
2078
|
+
this.server.use(this.jsonServer.rewriter(this.redirectUrls));
|
|
2079
|
+
Object.keys(this.redirectUrls).forEach((redirectUrl) => {
|
|
2080
|
+
this.useCors(redirectUrl);
|
|
2081
|
+
});
|
|
2082
|
+
this.server.use(middlewares);
|
|
2083
|
+
this.server.use(router);
|
|
2084
|
+
router.render = function(req, res) {
|
|
2085
|
+
res.jsonp({
|
|
2086
|
+
content: res.locals.data
|
|
2087
|
+
});
|
|
2088
|
+
};
|
|
2089
|
+
this.server.listen(this.port, () => {
|
|
2090
|
+
console.log("JSON Server is running on port " + this.port);
|
|
2091
|
+
});
|
|
2092
|
+
}
|
|
2093
|
+
useCors(url) {
|
|
2094
|
+
console.log("Configuring CORS for: ", url);
|
|
2095
|
+
this.server.use(url, _MockServerBuilder.allowCors);
|
|
2096
|
+
}
|
|
2097
|
+
static allowCors(req, res, next) {
|
|
2098
|
+
res.header("Access-Control-Allow-Origin", "*");
|
|
2099
|
+
res.header("Access-Control-Allow-Headers", "*");
|
|
2100
|
+
res.header("Access-Control-Allow-Methods", "*");
|
|
2101
|
+
next();
|
|
2102
|
+
}
|
|
2103
|
+
static postRewriteToGet(req, res, next) {
|
|
2104
|
+
if (req.method === "POST") {
|
|
2105
|
+
console.log(`Received POST request, rewriting to GET: ` + req.url);
|
|
2106
|
+
req.method = "GET";
|
|
2107
|
+
}
|
|
2108
|
+
next();
|
|
2109
|
+
}
|
|
2110
|
+
};
|
|
1977
2111
|
export {
|
|
1978
2112
|
CSAlert,
|
|
1979
2113
|
CSBox,
|
|
@@ -2029,6 +2163,7 @@ export {
|
|
|
2029
2163
|
MessageCodeTO,
|
|
2030
2164
|
MessageService,
|
|
2031
2165
|
MessageSeverity,
|
|
2166
|
+
MockServerBuilder,
|
|
2032
2167
|
NO_HTTP_CODE,
|
|
2033
2168
|
NSK_CONTENT,
|
|
2034
2169
|
ObjectWithId,
|