@docbox-nz/hapi-gateway 0.1.0 → 0.2.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/dist/options.d.ts CHANGED
@@ -42,6 +42,12 @@ export interface PluginOptions {
42
42
  * Additional configuration options for axios
43
43
  */
44
44
  axiosConfig?: CreateAxiosDefaults;
45
+ /**
46
+ * Get a API key for making requests to docbox
47
+ *
48
+ * @returns The key or a promise to the key
49
+ */
50
+ getApiKey?: () => (string | null) | Promise<string | null>;
45
51
  /**
46
52
  * Write access check, will be invoked to check whether the request
47
53
  * has authorization to perform a write action against docbox
package/package.json CHANGED
@@ -1,56 +1,55 @@
1
- {
2
- "name": "@docbox-nz/hapi-gateway",
3
- "version": "0.1.0",
4
- "type": "module",
5
- "scripts": {
6
- "build": "rollup -c",
7
- "prepublishOnly": "npm run build",
8
- "watch": "rollup -c --watch"
9
- },
10
- "main": "./dist/index.browser.cjs",
11
- "types": "./dist/index.d.ts",
12
- "exports": {
13
- ".": {
14
- "types": {
15
- "default": "./dist/index.d.ts"
16
- },
17
- "default": {
18
- "require": "./dist/index.node.cjs",
19
- "default": "./dist/index.node.esm.js"
20
- }
21
- }
22
- },
23
- "author": "Jacobtread",
24
- "license": "MIT",
25
- "description": "Docbox hapi server gateway",
26
- "dependencies": {
27
- "@hapi/boom": "^10.0.1",
28
- "axios": "^1.7.4"
29
- },
30
- "peerDependencies": {
31
- "@hapi/hapi": "^20.1.0 || ^21.0.0"
32
- },
33
- "devDependencies": {
34
- "@hapi/boom": "^10.0.1",
35
- "@hapi/hapi": "^21.4.0",
36
- "@rollup/plugin-commonjs": "^28.0.3",
37
- "@rollup/plugin-json": "^6.1.0",
38
- "@rollup/plugin-node-resolve": "^16.0.1",
39
- "@types/node": "^24.0.0",
40
- "@typescript-eslint/eslint-plugin": "^7.18.0",
41
- "@typescript-eslint/parser": "^7.18.0",
42
- "eslint": "^8.55.0",
43
- "eslint-config-airbnb": "^19.0.4",
44
- "eslint-config-airbnb-typescript": "^18.0.0",
45
- "eslint-config-prettier": "^9.1.0",
46
- "eslint-import-resolver-typescript": "^3.6.1",
47
- "eslint-plugin-import": "^2.29.0",
48
- "eslint-plugin-perfectionist": "^2.5.0",
49
- "eslint-plugin-prettier": "^5.0.1",
50
- "eslint-plugin-unused-imports": "^3.0.0",
51
- "prettier": "^3.1.0",
52
- "rollup-plugin-typescript2": "^0.36.0",
53
- "tslib": "^2.8.1",
54
- "typescript": "^5.8.3"
55
- }
1
+ {
2
+ "name": "@docbox-nz/hapi-gateway",
3
+ "version": "0.2.1",
4
+ "type": "module",
5
+ "main": "./dist/index.browser.cjs",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": {
10
+ "default": "./dist/index.d.ts"
11
+ },
12
+ "default": {
13
+ "require": "./dist/index.node.cjs",
14
+ "default": "./dist/index.node.esm.js"
15
+ }
16
+ }
17
+ },
18
+ "author": "Jacobtread",
19
+ "license": "MIT",
20
+ "description": "Docbox hapi server gateway",
21
+ "dependencies": {
22
+ "@hapi/boom": "^10.0.1",
23
+ "axios": "^1.7.4"
24
+ },
25
+ "peerDependencies": {
26
+ "@hapi/hapi": "^20.1.0 || ^21.0.0"
27
+ },
28
+ "devDependencies": {
29
+ "@hapi/boom": "^10.0.1",
30
+ "@hapi/hapi": "^21.4.0",
31
+ "@rollup/plugin-commonjs": "^28.0.3",
32
+ "@rollup/plugin-json": "^6.1.0",
33
+ "@rollup/plugin-node-resolve": "^16.0.1",
34
+ "@types/node": "^24.0.0",
35
+ "@typescript-eslint/eslint-plugin": "^7.18.0",
36
+ "@typescript-eslint/parser": "^7.18.0",
37
+ "eslint": "^8.55.0",
38
+ "eslint-config-airbnb": "^19.0.4",
39
+ "eslint-config-airbnb-typescript": "^18.0.0",
40
+ "eslint-config-prettier": "^9.1.0",
41
+ "eslint-import-resolver-typescript": "^3.6.1",
42
+ "eslint-plugin-import": "^2.29.0",
43
+ "eslint-plugin-perfectionist": "^2.5.0",
44
+ "eslint-plugin-prettier": "^5.0.1",
45
+ "eslint-plugin-unused-imports": "^3.0.0",
46
+ "prettier": "^3.1.0",
47
+ "rollup-plugin-typescript2": "^0.37.0",
48
+ "tslib": "^2.8.1",
49
+ "typescript": "^5.8.3"
50
+ },
51
+ "scripts": {
52
+ "build": "rollup -c",
53
+ "watch": "rollup -c --watch"
54
+ }
56
55
  }
package/src/index.ts CHANGED
@@ -44,6 +44,7 @@ export function createDocboxRoutes(options: PluginOptions) {
44
44
  );
45
45
 
46
46
  const getRequestUser: PluginOptions['getRequestUser'] = options.getRequestUser ?? (() => null);
47
+ const getApiKey: PluginOptions['getApiKey'] = options.getApiKey ?? (() => null);
47
48
 
48
49
  // Base route for creating document boxes
49
50
  const createBoxRoute: ServerRoute = {
@@ -68,7 +69,9 @@ export function createDocboxRoutes(options: PluginOptions) {
68
69
 
69
70
  const tenant = await options.getRequestTenant(request);
70
71
  const user = await getRequestUser(request);
71
- return forwardRequest(axiosInstance, request, tenant, user, h, path);
72
+ const apiKey = await getApiKey();
73
+
74
+ return forwardRequest(axiosInstance, request, tenant, user, apiKey, h, path);
72
75
  },
73
76
  options: baseRouteOptions,
74
77
  };
@@ -91,7 +94,9 @@ export function createDocboxRoutes(options: PluginOptions) {
91
94
 
92
95
  const tenant = await options.getRequestTenant(request);
93
96
  const user = await getRequestUser(request);
94
- return forwardRequest(axiosInstance, request, tenant, user, h, path);
97
+ const apiKey = await getApiKey();
98
+
99
+ return forwardRequest(axiosInstance, request, tenant, user, apiKey, h, path);
95
100
  },
96
101
  options: { ...baseRouteOptions, ...baseForwardRouteOptions },
97
102
  };
@@ -120,7 +125,9 @@ export function createDocboxRoutes(options: PluginOptions) {
120
125
 
121
126
  const tenant = await options.getRequestTenant(request);
122
127
  const user = await getRequestUser(request);
123
- return forwardRequest(axiosInstance, request, tenant, user, h, path);
128
+ const apiKey = await getApiKey();
129
+
130
+ return forwardRequest(axiosInstance, request, tenant, user, apiKey, h, path);
124
131
  },
125
132
  options: {
126
133
  ...baseRouteOptions,
@@ -176,6 +183,7 @@ function isWriteRequest(request: Request, path: string) {
176
183
  * @param axios Axios instance to forward the request through
177
184
  * @param request The request itself
178
185
  * @param user User performing the request
186
+ * @param apiKey API key for requests
179
187
  * @param h Response toolkit for creating the response
180
188
  * @param path Path requested from the server
181
189
  */
@@ -184,12 +192,17 @@ async function forwardRequest(
184
192
  request: Request,
185
193
  tenant: DocboxRequestTenant,
186
194
  user: DocboxRequestUser | null,
195
+ apiKey: string | null,
187
196
  h: ResponseToolkit,
188
197
  path: string
189
198
  ) {
190
199
  const userHeaders: Record<string, any> = request.headers;
191
200
  const forwardHeaders: Record<string, any> = {};
192
201
 
202
+ if (apiKey !== null) {
203
+ forwardHeaders['x-docbox-api-key'] = apiKey;
204
+ }
205
+
193
206
  // Forward content related headers
194
207
  if (userHeaders['accept'] !== undefined) {
195
208
  forwardHeaders['accept'] = userHeaders['accept'];
package/src/options.ts CHANGED
@@ -53,6 +53,13 @@ export interface PluginOptions {
53
53
  */
54
54
  axiosConfig?: CreateAxiosDefaults;
55
55
 
56
+ /**
57
+ * Get a API key for making requests to docbox
58
+ *
59
+ * @returns The key or a promise to the key
60
+ */
61
+ getApiKey?: () => (string | null) | Promise<string | null>;
62
+
56
63
  /**
57
64
  * Write access check, will be invoked to check whether the request
58
65
  * has authorization to perform a write action against docbox