@hashrytech/quick-components-kit 0.14.1 → 0.14.2
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 +6 -0
- package/dist/modules/api-proxy.d.ts +12 -10
- package/dist/modules/api-proxy.js +32 -28
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,19 +2,21 @@ import type { RequestHandler } from '@sveltejs/kit';
|
|
|
2
2
|
/**
|
|
3
3
|
* Configuration interface for the reusable proxy module.
|
|
4
4
|
*/
|
|
5
|
-
export interface
|
|
6
|
-
/** Your API
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
allowedPaths
|
|
10
|
-
/** Optional extra headers to forward */
|
|
11
|
-
|
|
12
|
-
/** Optional extra headers to
|
|
13
|
-
|
|
5
|
+
export interface RestApiProxyConfig {
|
|
6
|
+
/** Your API HOST URL (e.g. 'https://api.example.com') */
|
|
7
|
+
host: string;
|
|
8
|
+
/** Optional array of allowed path prefixes to restrict access to downstream paths. Example: v1/products */
|
|
9
|
+
allowedPaths?: string[];
|
|
10
|
+
/** Optional extra headers to forward from the request */
|
|
11
|
+
safeRequestHeaders?: string[];
|
|
12
|
+
/** Optional extra headers to forward in the response */
|
|
13
|
+
safeResponseHeaders?: string[];
|
|
14
14
|
/** Optional function to extract token from session */
|
|
15
15
|
extractToken?: (locals: App.Locals) => string | undefined;
|
|
16
16
|
}
|
|
17
|
+
export declare const defaultRequestHeaders: string[];
|
|
18
|
+
export declare const defaultResponseHeaders: string[];
|
|
17
19
|
/**
|
|
18
20
|
* Creates a set of SvelteKit request handlers for proxying API calls securely.
|
|
19
21
|
*/
|
|
20
|
-
export declare function
|
|
22
|
+
export declare function createProxyHandlers(config: RestApiProxyConfig): Record<string, RequestHandler>;
|
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
import { error } from '@sveltejs/kit';
|
|
2
|
+
export const defaultRequestHeaders = ['authorization', 'content-type', 'accept', 'accept-language', 'cookie', 'x-csrftoken', 'referer', 'user-agent', 'x-requested-with'];
|
|
3
|
+
export const defaultResponseHeaders = ['content-type', 'content-length', 'cache-control', 'etag'];
|
|
2
4
|
/**
|
|
3
5
|
* Creates a set of SvelteKit request handlers for proxying API calls securely.
|
|
4
6
|
*/
|
|
5
|
-
export function
|
|
6
|
-
const safeRequestHeaders = [
|
|
7
|
-
'authorization', 'content-type', 'accept', 'accept-language',
|
|
8
|
-
'cookie', 'x-csrftoken', 'referer', 'user-agent', 'x-requested-with',
|
|
9
|
-
...(config.extraRequestHeaders ?? [])
|
|
10
|
-
];
|
|
11
|
-
const safeResponseHeaders = [
|
|
12
|
-
'content-type', 'content-length', 'cache-control', 'etag',
|
|
13
|
-
...(config.extraResponseHeaders ?? [])
|
|
14
|
-
];
|
|
7
|
+
export function createProxyHandlers(config) {
|
|
15
8
|
async function handler(event) {
|
|
16
9
|
const path = event.params.path;
|
|
17
|
-
const
|
|
18
|
-
const fullPath = `/${path}${
|
|
19
|
-
const
|
|
20
|
-
if
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
10
|
+
const queryString = event.url.searchParams.toString();
|
|
11
|
+
const fullPath = `/${path}${queryString ? `?${queryString}` : ''}`;
|
|
12
|
+
const url = `${config.host}${fullPath}`;
|
|
13
|
+
// Validate the path against allowed prefixes if specified
|
|
14
|
+
if (config.allowedPaths && config.allowedPaths.length > 0) {
|
|
15
|
+
const isAllowed = config.allowedPaths.some((prefix) => path?.startsWith(prefix));
|
|
16
|
+
if (!isAllowed)
|
|
17
|
+
throw error(403, 'Forbidden: This API path is not allowed.');
|
|
18
|
+
}
|
|
19
|
+
let headers = new Headers(event.request.headers);
|
|
20
|
+
if (config.safeRequestHeaders && config.safeRequestHeaders.length > 0) {
|
|
21
|
+
headers = new Headers();
|
|
22
|
+
for (const [key, value] of event.request.headers) {
|
|
23
|
+
if (defaultRequestHeaders.includes(key.toLowerCase()) && value != null) {
|
|
24
|
+
headers.set(key, value);
|
|
25
|
+
}
|
|
27
26
|
}
|
|
28
27
|
}
|
|
29
28
|
const token = config.extractToken?.(event.locals);
|
|
@@ -36,16 +35,21 @@ export function createApiProxyHandlers(config) {
|
|
|
36
35
|
? await event.request.clone().arrayBuffer()
|
|
37
36
|
: undefined
|
|
38
37
|
});
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
if (config.safeResponseHeaders && config.safeResponseHeaders.length > 0) {
|
|
39
|
+
const responseHeaders = new Headers();
|
|
40
|
+
for (const [key, value] of proxyResponse.headers) {
|
|
41
|
+
if (config.safeResponseHeaders.includes(key.toLowerCase()) && value != null) {
|
|
42
|
+
responseHeaders.set(key, value);
|
|
43
|
+
}
|
|
43
44
|
}
|
|
45
|
+
return new Response(proxyResponse.body, {
|
|
46
|
+
status: proxyResponse.status,
|
|
47
|
+
headers: responseHeaders
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return proxyResponse;
|
|
44
52
|
}
|
|
45
|
-
return new Response(proxyResponse.body, {
|
|
46
|
-
status: proxyResponse.status,
|
|
47
|
-
headers: responseHeaders
|
|
48
|
-
});
|
|
49
53
|
}
|
|
50
54
|
return {
|
|
51
55
|
GET: handler,
|