@docbox-nz/hapi-gateway 0.1.0 → 0.2.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/README.md +1 -1
- package/dist/index.node.cjs +13 -5
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.esm.js +13 -5
- package/dist/index.node.esm.js.map +1 -1
- package/dist/options.d.ts +6 -0
- package/package.json +1 -1
- package/src/index.ts +16 -3
- package/src/options.ts +7 -0
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
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|