@canva/cli 1.10.0 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/README.md +2 -0
- package/cli.js +596 -574
- package/lib/cjs/index.cjs +2 -2
- package/lib/esm/index.mjs +2 -2
- package/lib/index.d.ts +2 -0
- package/package.json +7 -2
- package/templates/base/package.json +9 -8
- package/templates/base/styles/components.css +18 -0
- package/templates/common/.env.template +1 -1
- package/templates/common/jest.config.mjs +1 -1
- package/templates/dam/backend/server.ts +8 -0
- package/templates/dam/canva-app.json +9 -0
- package/templates/dam/package.json +10 -8
- package/templates/dam/src/index.tsx +3 -21
- package/templates/dam/src/intents/design_editor/index.tsx +25 -0
- package/templates/data_connector/README.md +1 -1
- package/templates/data_connector/package.json +9 -8
- package/templates/data_connector/src/api/data_sources/designs.tsx +1 -1
- package/templates/data_connector/src/api/data_sources/templates.tsx +1 -1
- package/templates/data_connector/src/components/header.tsx +1 -1
- package/templates/data_connector/src/index.tsx +2 -66
- package/templates/data_connector/src/{app.tsx → intents/data_connector/app.tsx} +3 -3
- package/templates/data_connector/src/{entrypoint.tsx → intents/data_connector/entrypoint.tsx} +5 -5
- package/templates/data_connector/src/{home.tsx → intents/data_connector/home.tsx} +1 -1
- package/templates/data_connector/src/intents/data_connector/index.tsx +56 -0
- package/templates/data_connector/src/pages/error.tsx +1 -1
- package/templates/data_connector/src/pages/login.tsx +1 -1
- package/templates/data_connector/src/routes/protected_route.tsx +1 -1
- package/templates/data_connector/src/routes/routes.tsx +3 -3
- package/templates/data_connector/styles/components.css +18 -0
- package/templates/gen_ai/backend/server.ts +17 -0
- package/templates/gen_ai/canva-app.json +5 -0
- package/templates/gen_ai/package.json +10 -8
- package/templates/gen_ai/src/api/api.ts +4 -0
- package/templates/gen_ai/src/components/footer.tsx +1 -1
- package/templates/gen_ai/src/components/loading_results.tsx +1 -1
- package/templates/gen_ai/src/components/prompt_input.tsx +1 -1
- package/templates/gen_ai/src/index.tsx +3 -14
- package/templates/gen_ai/src/{app.tsx → intents/design_editor/app.tsx} +3 -3
- package/templates/gen_ai/src/{home.tsx → intents/design_editor/home.tsx} +1 -1
- package/templates/gen_ai/src/intents/design_editor/index.tsx +17 -0
- package/templates/gen_ai/src/pages/error.tsx +1 -1
- package/templates/gen_ai/src/routes/routes.tsx +2 -2
- package/templates/gen_ai/styles/components.css +18 -0
- package/templates/hello_world/canva-app.json +5 -0
- package/templates/hello_world/package.json +10 -8
- package/templates/hello_world/src/index.tsx +3 -21
- package/templates/hello_world/src/{app.tsx → intents/design_editor/app.tsx} +26 -3
- package/templates/hello_world/src/intents/design_editor/index.tsx +25 -0
- package/templates/hello_world/src/{tests → intents/design_editor/tests}/app.tests.tsx +19 -13
- package/templates/hello_world/styles/components.css +18 -0
- package/templates/mls/README.md +81 -0
- package/templates/mls/canva-app.json +25 -0
- package/templates/mls/declarations/declarations.d.ts +29 -0
- package/templates/mls/eslint.config.mjs +14 -0
- package/templates/mls/jest.config.mjs +36 -0
- package/templates/mls/jest.setup.ts +37 -0
- package/templates/mls/package.json +117 -0
- package/templates/mls/scripts/copy_env.ts +13 -0
- package/templates/mls/scripts/ssl/ssl.ts +131 -0
- package/templates/mls/scripts/start/app_runner.ts +223 -0
- package/templates/mls/scripts/start/context.ts +171 -0
- package/templates/mls/scripts/start/start.ts +46 -0
- package/templates/mls/src/__tests__/app.tests.tsx +11 -0
- package/templates/mls/src/__tests__/office_selection_page.tests.tsx +72 -0
- package/templates/mls/src/__tests__/utils.tsx +19 -0
- package/templates/mls/src/adapter.ts +126 -0
- package/templates/mls/src/components/agent/agent_card.tsx +57 -0
- package/templates/mls/src/components/agent/agent_grid.tsx +37 -0
- package/templates/mls/src/components/agent/agent_list.tsx +17 -0
- package/templates/mls/src/components/agent/agent_search_filters.tsx +88 -0
- package/templates/mls/src/components/breadcrumb/breadcrumb.tsx +40 -0
- package/templates/mls/src/components/listing/listing_card.tsx +64 -0
- package/templates/mls/src/components/listing/listing_grid.tsx +37 -0
- package/templates/mls/src/components/listing/listing_list.tsx +21 -0
- package/templates/mls/src/components/listing/listing_search_filters.tsx +145 -0
- package/templates/mls/src/components/placeholders/placeholders.tsx +65 -0
- package/templates/mls/src/data.ts +359 -0
- package/templates/mls/src/index.tsx +4 -0
- package/templates/mls/src/intents/design_editor/app.tsx +44 -0
- package/templates/mls/src/intents/design_editor/index.tsx +25 -0
- package/templates/mls/src/pages/agent_details_page/agent_details_page.tsx +175 -0
- package/templates/mls/src/pages/list_page/agent_tab_panel.tsx +126 -0
- package/templates/mls/src/pages/list_page/list_page.tsx +67 -0
- package/templates/mls/src/pages/list_page/listing_tab_panel.tsx +135 -0
- package/templates/mls/src/pages/listing_details_page/listing_details_page.tsx +418 -0
- package/templates/mls/src/pages/loading_page/loading_page.tsx +152 -0
- package/templates/mls/src/pages/office_selection_page/office_selection_page.tsx +144 -0
- package/templates/mls/src/real_estate.type.ts +44 -0
- package/templates/mls/src/util/use_add_element.tsx +62 -0
- package/templates/mls/src/util/use_drag_element.tsx +68 -0
- package/templates/mls/styles/components.css +56 -0
- package/templates/mls/tsconfig.json +55 -0
- package/templates/mls/webpack.config.ts +254 -0
- package/templates/optional/AGENTS.md +80 -2
- package/templates/optional/CLAUDE.md +80 -2
- package/templates/base/backend/routers/oauth.ts +0 -393
- package/templates/base/utils/backend/bearer_middleware/bearer_middleware.ts +0 -99
- package/templates/base/utils/backend/bearer_middleware/index.ts +0 -1
- package/templates/base/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +0 -192
- package/templates/base/utils/use_add_element.ts +0 -58
- package/templates/base/utils/use_feature_support.ts +0 -28
- package/templates/common/utils/backend/base_backend/create.ts +0 -104
- package/templates/common/utils/table_wrapper.ts +0 -520
- package/templates/common/utils/use_add_element.ts +0 -58
- package/templates/common/utils/use_feature_support.ts +0 -28
- package/templates/common/utils/use_overlay_hook.ts +0 -76
- package/templates/common/utils/use_selection_hook.ts +0 -37
- package/templates/gen_ai/backend/database/database.ts +0 -42
- package/templates/gen_ai/utils/backend/bearer_middleware/bearer_middleware.ts +0 -99
- package/templates/gen_ai/utils/backend/bearer_middleware/index.ts +0 -1
- package/templates/hello_world/utils/use_add_element.ts +0 -58
- package/templates/hello_world/utils/use_feature_support.ts +0 -28
- /package/templates/base/{utils/backend → backend}/base_backend/create.ts +0 -0
- /package/templates/base/{utils/backend → backend}/jwt_middleware/index.ts +0 -0
- /package/templates/base/{utils/backend → backend}/jwt_middleware/jwt_middleware.ts +0 -0
- /package/templates/dam/src/{adapter.ts → intents/design_editor/adapter.ts} +0 -0
- /package/templates/dam/src/{app.tsx → intents/design_editor/app.tsx} +0 -0
- /package/templates/dam/src/{config.ts → intents/design_editor/config.ts} +0 -0
- /package/templates/dam/src/{index.css → intents/design_editor/index.css} +0 -0
- /package/templates/data_connector/src/{paths.ts → routes/paths.ts} +0 -0
- /package/templates/gen_ai/src/{paths.ts → routes/paths.ts} +0 -0
- /package/templates/{common → gen_ai}/utils/backend/jwt_middleware/index.ts +0 -0
- /package/templates/{common → gen_ai}/utils/backend/jwt_middleware/jwt_middleware.ts +0 -0
- /package/templates/hello_world/src/{tests → intents/design_editor/tests}/__snapshots__/app.tests.tsx.snap +0 -0
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
2
|
-
import type { NextFunction, Request, Response } from "express";
|
|
3
|
-
import type {
|
|
4
|
-
createBearerMiddleware,
|
|
5
|
-
GetTokenFromRequest,
|
|
6
|
-
} from "../bearer_middleware";
|
|
7
|
-
|
|
8
|
-
type Middleware = (req: Request, res: Response, next: NextFunction) => void;
|
|
9
|
-
|
|
10
|
-
describe("createBearerMiddleware", () => {
|
|
11
|
-
let fakeGetTokenFromRequest: jest.MockedFn<GetTokenFromRequest>;
|
|
12
|
-
let verify: jest.MockedFn<(token: string) => Promise<string | undefined>>;
|
|
13
|
-
|
|
14
|
-
let req: Request;
|
|
15
|
-
let res: Response;
|
|
16
|
-
let next: jest.MockedFn<() => void>;
|
|
17
|
-
|
|
18
|
-
let AuthorizationError: typeof Error;
|
|
19
|
-
let createBearerMiddlewareFn: typeof createBearerMiddleware;
|
|
20
|
-
let bearerMiddleware: Middleware;
|
|
21
|
-
|
|
22
|
-
beforeEach(() => {
|
|
23
|
-
jest.resetAllMocks();
|
|
24
|
-
jest.resetModules();
|
|
25
|
-
|
|
26
|
-
fakeGetTokenFromRequest = jest.fn();
|
|
27
|
-
verify = jest.fn();
|
|
28
|
-
|
|
29
|
-
const middlewareModule = require("../bearer_middleware");
|
|
30
|
-
createBearerMiddlewareFn = middlewareModule.createBearerMiddleware;
|
|
31
|
-
AuthorizationError = middlewareModule.AuthorizationError;
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
describe("When called", () => {
|
|
35
|
-
beforeEach(() => {
|
|
36
|
-
req = {
|
|
37
|
-
header: (_name: string) => undefined,
|
|
38
|
-
} as Request;
|
|
39
|
-
|
|
40
|
-
res = {
|
|
41
|
-
status: jest.fn().mockReturnThis(),
|
|
42
|
-
json: jest.fn().mockReturnThis(),
|
|
43
|
-
send: jest.fn().mockReturnThis(),
|
|
44
|
-
} as unknown as Response;
|
|
45
|
-
|
|
46
|
-
next = jest.fn();
|
|
47
|
-
|
|
48
|
-
bearerMiddleware = createBearerMiddlewareFn(
|
|
49
|
-
verify,
|
|
50
|
-
fakeGetTokenFromRequest,
|
|
51
|
-
);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe("When `getTokenFromRequest` throws an exception ('Fake error')", () => {
|
|
55
|
-
beforeEach(() => {
|
|
56
|
-
fakeGetTokenFromRequest.mockRejectedValue(
|
|
57
|
-
new AuthorizationError("Fake error"),
|
|
58
|
-
);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it(`Does not call next() and returns HTTP 401 with error = "unauthorized" and message = "Fake error"`, async () => {
|
|
62
|
-
expect.assertions(8);
|
|
63
|
-
|
|
64
|
-
expect(fakeGetTokenFromRequest).not.toHaveBeenCalled();
|
|
65
|
-
await bearerMiddleware(req, res, next);
|
|
66
|
-
|
|
67
|
-
expect(fakeGetTokenFromRequest).toHaveBeenCalledTimes(1);
|
|
68
|
-
expect(fakeGetTokenFromRequest).toHaveBeenLastCalledWith(req);
|
|
69
|
-
|
|
70
|
-
expect(res.status).toHaveBeenCalledTimes(1);
|
|
71
|
-
expect(res.status).toHaveBeenLastCalledWith(401);
|
|
72
|
-
|
|
73
|
-
expect(res.json).toHaveBeenCalledTimes(1);
|
|
74
|
-
expect(res.json).toHaveBeenLastCalledWith({
|
|
75
|
-
error: "unauthorized",
|
|
76
|
-
message: "Fake error",
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
expect(next).not.toHaveBeenCalled();
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe("When the middleware cannot verify the token", () => {
|
|
84
|
-
beforeEach(() => {
|
|
85
|
-
fakeGetTokenFromRequest.mockReturnValue("TOKEN");
|
|
86
|
-
|
|
87
|
-
verify.mockImplementation(() => Promise.resolve(undefined));
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it(`Does not call next() and returns HTTP 401 with error = "unauthorized" and message = "Token is invalid"`, async () => {
|
|
91
|
-
expect.assertions(5);
|
|
92
|
-
|
|
93
|
-
await bearerMiddleware(req, res, next);
|
|
94
|
-
|
|
95
|
-
expect(res.status).toHaveBeenCalledTimes(1);
|
|
96
|
-
expect(res.status).toHaveBeenLastCalledWith(401);
|
|
97
|
-
|
|
98
|
-
expect(res.json).toHaveBeenCalledTimes(1);
|
|
99
|
-
expect(res.json).toHaveBeenLastCalledWith({
|
|
100
|
-
error: "unauthorized",
|
|
101
|
-
message: "Token is invalid",
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
expect(next).not.toHaveBeenCalled();
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
describe("getTokenFromHttpHeader", () => {
|
|
111
|
-
let getHeader: jest.MockedFn<(name: string) => string | undefined>;
|
|
112
|
-
let req: Request;
|
|
113
|
-
let getTokenFromHttpHeader: (req: Request) => string;
|
|
114
|
-
let AuthorizationError: typeof Error;
|
|
115
|
-
|
|
116
|
-
beforeEach(() => {
|
|
117
|
-
getHeader = jest.fn();
|
|
118
|
-
req = {
|
|
119
|
-
header: (name: string) => getHeader(name),
|
|
120
|
-
} as Request;
|
|
121
|
-
|
|
122
|
-
const bearerMiddlewareModule = require("../bearer_middleware");
|
|
123
|
-
getTokenFromHttpHeader = bearerMiddlewareModule.getTokenFromHttpHeader;
|
|
124
|
-
AuthorizationError = bearerMiddlewareModule.AuthorizationError;
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
describe("When the 'Authorization' header is missing", () => {
|
|
128
|
-
beforeEach(() => {
|
|
129
|
-
getHeader.mockReturnValue(undefined);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it(`Throws a AuthorizationError with message = 'Missing the "Authorization" header'`, async () => {
|
|
133
|
-
expect.assertions(3);
|
|
134
|
-
|
|
135
|
-
expect(() => getTokenFromHttpHeader(req)).toThrow(
|
|
136
|
-
new AuthorizationError('Missing the "Authorization" header'),
|
|
137
|
-
);
|
|
138
|
-
expect(getHeader).toHaveBeenCalledTimes(1);
|
|
139
|
-
expect(getHeader).toHaveBeenLastCalledWith("Authorization");
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe("When the 'Authorization' header doesn't have a Bearer scheme", () => {
|
|
144
|
-
beforeEach(() => {
|
|
145
|
-
getHeader.mockReturnValue("Beerer FAKE_TOKEN");
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it(`Throws a AuthorizationError with message = 'Missing a "Bearer" token in the "Authorization" header''`, async () => {
|
|
149
|
-
expect.assertions(3);
|
|
150
|
-
|
|
151
|
-
expect(() => getTokenFromHttpHeader(req)).toThrow(
|
|
152
|
-
new AuthorizationError(
|
|
153
|
-
'Missing a "Bearer" token in the "Authorization" header',
|
|
154
|
-
),
|
|
155
|
-
);
|
|
156
|
-
expect(getHeader).toHaveBeenCalledTimes(1);
|
|
157
|
-
expect(getHeader).toHaveBeenLastCalledWith("Authorization");
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
describe("When the 'Authorization' Bearer scheme header doesn't have a token", () => {
|
|
162
|
-
beforeEach(() => {
|
|
163
|
-
getHeader.mockReturnValue("Bearer ");
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it(`Throws a AuthorizationError with message = 'Missing a "Bearer" token in the "Authorization" header'`, async () => {
|
|
167
|
-
expect.assertions(3);
|
|
168
|
-
|
|
169
|
-
expect(() => getTokenFromHttpHeader(req)).toThrow(
|
|
170
|
-
new AuthorizationError(
|
|
171
|
-
'Missing a "Bearer" token in the "Authorization" header',
|
|
172
|
-
),
|
|
173
|
-
);
|
|
174
|
-
expect(getHeader).toHaveBeenCalledTimes(1);
|
|
175
|
-
expect(getHeader).toHaveBeenLastCalledWith("Authorization");
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
describe("When the 'Authorization' Bearer scheme header has a token", () => {
|
|
180
|
-
beforeEach(() => {
|
|
181
|
-
getHeader.mockReturnValue("Bearer TOKEN");
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it(`Returns the token`, async () => {
|
|
185
|
-
expect.assertions(3);
|
|
186
|
-
|
|
187
|
-
expect(getTokenFromHttpHeader(req)).toEqual("TOKEN");
|
|
188
|
-
expect(getHeader).toHaveBeenCalledTimes(1);
|
|
189
|
-
expect(getHeader).toHaveBeenLastCalledWith("Authorization");
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
});
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
EmbedElement,
|
|
3
|
-
ImageElement,
|
|
4
|
-
RichtextElement,
|
|
5
|
-
TableElement,
|
|
6
|
-
TextElement,
|
|
7
|
-
VideoElement,
|
|
8
|
-
} from "@canva/design";
|
|
9
|
-
import { addElementAtCursor, addElementAtPoint } from "@canva/design";
|
|
10
|
-
import { features } from "@canva/platform";
|
|
11
|
-
import { useEffect, useState } from "react";
|
|
12
|
-
import { useFeatureSupport } from "./use_feature_support";
|
|
13
|
-
|
|
14
|
-
type AddElementParams =
|
|
15
|
-
| ImageElement
|
|
16
|
-
| VideoElement
|
|
17
|
-
| EmbedElement
|
|
18
|
-
| TextElement
|
|
19
|
-
| RichtextElement
|
|
20
|
-
| TableElement;
|
|
21
|
-
|
|
22
|
-
export const useAddElement = () => {
|
|
23
|
-
const isSupported = useFeatureSupport();
|
|
24
|
-
|
|
25
|
-
// Store a wrapped addElement function that checks feature support
|
|
26
|
-
const [addElement, setAddElement] = useState(() => {
|
|
27
|
-
return (element: AddElementParams) => {
|
|
28
|
-
if (features.isSupported(addElementAtPoint)) {
|
|
29
|
-
return addElementAtPoint(element);
|
|
30
|
-
} else if (features.isSupported(addElementAtCursor)) {
|
|
31
|
-
return addElementAtCursor(element);
|
|
32
|
-
}
|
|
33
|
-
// eslint-disable-next-line no-console
|
|
34
|
-
console.warn(
|
|
35
|
-
"Neither addElementAtPoint nor addElementAtCursor are supported",
|
|
36
|
-
);
|
|
37
|
-
return Promise.resolve();
|
|
38
|
-
};
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
useEffect(() => {
|
|
42
|
-
const addElement = (element: AddElementParams) => {
|
|
43
|
-
if (isSupported(addElementAtPoint)) {
|
|
44
|
-
return addElementAtPoint(element);
|
|
45
|
-
} else if (isSupported(addElementAtCursor)) {
|
|
46
|
-
return addElementAtCursor(element);
|
|
47
|
-
}
|
|
48
|
-
// eslint-disable-next-line no-console
|
|
49
|
-
console.warn(
|
|
50
|
-
"Neither addElementAtPoint nor addElementAtCursor are supported",
|
|
51
|
-
);
|
|
52
|
-
return Promise.resolve();
|
|
53
|
-
};
|
|
54
|
-
setAddElement(() => addElement);
|
|
55
|
-
}, [isSupported]);
|
|
56
|
-
|
|
57
|
-
return addElement;
|
|
58
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { features } from "@canva/platform";
|
|
2
|
-
import type { Feature } from "@canva/platform";
|
|
3
|
-
import { useEffect, useState } from "react";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* This hook allows re-rendering of a React component whenever
|
|
7
|
-
* the state of feature support changes in Canva.
|
|
8
|
-
*
|
|
9
|
-
* @returns isSupported - callback to inspect a Canva SDK method.
|
|
10
|
-
**/
|
|
11
|
-
export function useFeatureSupport() {
|
|
12
|
-
// Store a wrapped function that checks feature support
|
|
13
|
-
const [isSupported, setIsSupported] = useState(() => {
|
|
14
|
-
return (...args: Feature[]) => features.isSupported(...args);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
// create new function ref when feature support changes to trigger
|
|
19
|
-
// re-render
|
|
20
|
-
return features.registerOnSupportChange(() => {
|
|
21
|
-
setIsSupported(() => {
|
|
22
|
-
return (...args: Feature[]) => features.isSupported(...args);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
}, []);
|
|
26
|
-
|
|
27
|
-
return isSupported;
|
|
28
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import debug from "debug";
|
|
3
|
-
import express from "express";
|
|
4
|
-
import type { NextFunction, Request, Response } from "express";
|
|
5
|
-
import fs from "fs";
|
|
6
|
-
import http from "http";
|
|
7
|
-
import https from "https";
|
|
8
|
-
|
|
9
|
-
const serverDebug = debug("server");
|
|
10
|
-
|
|
11
|
-
interface BaseServer {
|
|
12
|
-
app: express.Express;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Starts the server on the address or port provided
|
|
16
|
-
* @param address port number or string address or if left undefined express defaults to port 3000
|
|
17
|
-
*/
|
|
18
|
-
start: (address: number | string | undefined) => void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* createBaseServer instantiates a customised express server with:
|
|
23
|
-
* - json body handling
|
|
24
|
-
* - health check endpoint
|
|
25
|
-
* - catchall endpoint
|
|
26
|
-
* - error handler catch route
|
|
27
|
-
* - process termination handling
|
|
28
|
-
* - debug logging - prefix starting your server with `DEBUG=server npm run XXX`
|
|
29
|
-
*
|
|
30
|
-
* @returns BaseServer object containing the express app and a start function
|
|
31
|
-
*/
|
|
32
|
-
export function createBaseServer(router: express.Router): BaseServer {
|
|
33
|
-
const SHOULD_ENABLE_HTTPS = process.env?.SHOULD_ENABLE_HTTPS === "true";
|
|
34
|
-
const HTTPS_CERT_FILE = process.env?.HTTPS_CERT_FILE;
|
|
35
|
-
const HTTPS_KEY_FILE = process.env?.HTTPS_KEY_FILE;
|
|
36
|
-
|
|
37
|
-
const app = express();
|
|
38
|
-
app.use(express.json());
|
|
39
|
-
|
|
40
|
-
// It can help to provide an extra layer of obsecurity to reduce server fingerprinting.
|
|
41
|
-
app.disable("x-powered-by");
|
|
42
|
-
|
|
43
|
-
// Health check endpoint
|
|
44
|
-
app.get("/healthz", (req, res) => {
|
|
45
|
-
res.sendStatus(200);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// logging middleware
|
|
49
|
-
app.use((req, _res, next) => {
|
|
50
|
-
serverDebug(`${new Date().toISOString()}: ${req.method} ${req.url}`);
|
|
51
|
-
next();
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Custom routes router
|
|
55
|
-
app.use(router);
|
|
56
|
-
|
|
57
|
-
// catch all router
|
|
58
|
-
app.all("*", (req, res) => {
|
|
59
|
-
res.status(404).send({
|
|
60
|
-
error: `unhandled '${req.method}' on '${req.url}'`,
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// default error handler
|
|
65
|
-
app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
|
|
66
|
-
console.error(err.stack);
|
|
67
|
-
res.status(500).send({
|
|
68
|
-
error: "something went wrong",
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
let server;
|
|
73
|
-
if (SHOULD_ENABLE_HTTPS) {
|
|
74
|
-
if (!HTTPS_CERT_FILE || !HTTPS_KEY_FILE) {
|
|
75
|
-
throw new Error(
|
|
76
|
-
"Looks like you're running the example with --use-https flag, but SSL certificates haven't been generated. Please remove the .ssl/ folder and re-run the command again.",
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
server = https.createServer(
|
|
81
|
-
{
|
|
82
|
-
key: fs.readFileSync(HTTPS_KEY_FILE),
|
|
83
|
-
cert: fs.readFileSync(HTTPS_CERT_FILE),
|
|
84
|
-
},
|
|
85
|
-
app,
|
|
86
|
-
);
|
|
87
|
-
} else {
|
|
88
|
-
server = http.createServer(app);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return {
|
|
92
|
-
app,
|
|
93
|
-
start: (address: number | string | undefined) => {
|
|
94
|
-
console.log(`Listening on '${address}'`);
|
|
95
|
-
server.listen(address);
|
|
96
|
-
process.on("SIGTERM", () => {
|
|
97
|
-
serverDebug("SIGTERM signal received: closing HTTP server");
|
|
98
|
-
server.close(() => {
|
|
99
|
-
serverDebug("HTTP server closed");
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
}
|