@tanglemedia/svelte-starter-toolbelt 1.0.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 +58 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +1 -0
- package/dist/server/proxy/proxy.handle.d.ts +25 -0
- package/dist/server/proxy/proxy.handle.js +134 -0
- package/dist/service/index.d.ts +2 -0
- package/dist/service/index.js +2 -0
- package/dist/service/provider/create-directus-service.provider.d.ts +751 -0
- package/dist/service/provider/create-directus-service.provider.js +40 -0
- package/dist/service/service.factory.d.ts +34 -0
- package/dist/service/service.factory.js +49 -0
- package/dist/types/api-types.d.ts +40 -0
- package/dist/types/api-types.js +1 -0
- package/dist/types/helper-types.d.ts +8 -0
- package/dist/types/helper-types.js +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +1 -0
- package/dist/utility/deep-merge.d.ts +10 -0
- package/dist/utility/deep-merge.js +27 -0
- package/dist/utility/error/index.d.ts +1 -0
- package/dist/utility/error/index.js +1 -0
- package/dist/utility/error/server-error.d.ts +19 -0
- package/dist/utility/error/server-error.js +68 -0
- package/dist/utility/file/basename.d.ts +1 -0
- package/dist/utility/file/basename.js +9 -0
- package/dist/utility/file/file-to-base64.d.ts +1 -0
- package/dist/utility/file/file-to-base64.js +11 -0
- package/dist/utility/file/index.d.ts +3 -0
- package/dist/utility/file/index.js +3 -0
- package/dist/utility/file/url-to-base64.d.ts +1 -0
- package/dist/utility/file/url-to-base64.js +17 -0
- package/dist/utility/helper/is-writable.d.ts +3 -0
- package/dist/utility/helper/is-writable.js +6 -0
- package/dist/utility/index.d.ts +6 -0
- package/dist/utility/index.js +7 -0
- package/dist/utility/mutation/create-delete-mutation.d.ts +26 -0
- package/dist/utility/mutation/create-delete-mutation.js +35 -0
- package/dist/utility/mutation/create-insert-mutation.d.ts +12 -0
- package/dist/utility/mutation/create-insert-mutation.js +22 -0
- package/dist/utility/mutation/create-update-mutation.d.ts +12 -0
- package/dist/utility/mutation/create-update-mutation.js +23 -0
- package/dist/utility/mutation/index.d.ts +4 -0
- package/dist/utility/mutation/index.js +4 -0
- package/dist/utility/mutation/invalidate-collection-query.d.ts +2 -0
- package/dist/utility/mutation/invalidate-collection-query.js +12 -0
- package/dist/utility/persisted-store.d.ts +6 -0
- package/dist/utility/persisted-store.js +27 -0
- package/dist/utility/query/create-find-infinite-query.svelte.d.ts +10 -0
- package/dist/utility/query/create-find-infinite-query.svelte.js +37 -0
- package/dist/utility/query/create-find-one-query.svelte.d.ts +10 -0
- package/dist/utility/query/create-find-one-query.svelte.js +23 -0
- package/dist/utility/query/create-find-query.svelte.d.ts +17 -0
- package/dist/utility/query/create-find-query.svelte.js +32 -0
- package/dist/utility/query/index.d.ts +3 -0
- package/dist/utility/query/index.js +3 -0
- package/dist/utility/query-factory.d.ts +18 -0
- package/dist/utility/query-factory.js +34 -0
- package/package.json +79 -0
- package/src/app.d.ts +13 -0
- package/src/app.html +12 -0
- package/src/demo.spec.ts +7 -0
- package/src/index.ts +1 -0
- package/src/lib/index.ts +3 -0
- package/src/lib/server/index.ts +2 -0
- package/src/lib/server/proxy/proxy.handle.ts +210 -0
- package/src/lib/service/index.ts +2 -0
- package/src/lib/service/provider/create-directus-service.provider.ts +75 -0
- package/src/lib/service/service.factory.ts +93 -0
- package/src/lib/types/api-types.ts +56 -0
- package/src/lib/types/helper-types.ts +12 -0
- package/src/lib/types/index.ts +2 -0
- package/src/lib/utility/deep-merge.ts +37 -0
- package/src/lib/utility/error/index.ts +1 -0
- package/src/lib/utility/error/server-error.ts +105 -0
- package/src/lib/utility/file/basename.ts +10 -0
- package/src/lib/utility/file/file-to-base64.ts +11 -0
- package/src/lib/utility/file/index.ts +3 -0
- package/src/lib/utility/file/url-to-base64.ts +20 -0
- package/src/lib/utility/helper/is-writable.ts +9 -0
- package/src/lib/utility/index.ts +7 -0
- package/src/lib/utility/mutation/create-delete-mutation.ts +71 -0
- package/src/lib/utility/mutation/create-insert-mutation.ts +46 -0
- package/src/lib/utility/mutation/create-update-mutation.ts +47 -0
- package/src/lib/utility/mutation/index.ts +4 -0
- package/src/lib/utility/mutation/invalidate-collection-query.ts +19 -0
- package/src/lib/utility/persisted-store.ts +31 -0
- package/src/lib/utility/query/create-find-infinite-query.svelte.ts +71 -0
- package/src/lib/utility/query/create-find-one-query.svelte.ts +53 -0
- package/src/lib/utility/query/create-find-query.svelte.ts +57 -0
- package/src/lib/utility/query/index.ts +3 -0
- package/src/lib/utility/query-factory.ts +62 -0
- package/src/routes/+page.svelte +3 -0
- package/src/routes/page.svelte.test.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Svelte library
|
|
2
|
+
|
|
3
|
+
Everything you need to build a Svelte library, powered by [`sv`](https://npmjs.com/package/sv).
|
|
4
|
+
|
|
5
|
+
Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging).
|
|
6
|
+
|
|
7
|
+
## Creating a project
|
|
8
|
+
|
|
9
|
+
If you're seeing this, you've probably already done this step. Congrats!
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# create a new project in the current directory
|
|
13
|
+
npx sv create
|
|
14
|
+
|
|
15
|
+
# create a new project in my-app
|
|
16
|
+
npx sv create my-app
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Developing
|
|
20
|
+
|
|
21
|
+
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm run dev
|
|
25
|
+
|
|
26
|
+
# or start the server and open the app in a new browser tab
|
|
27
|
+
npm run dev -- --open
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
|
|
31
|
+
|
|
32
|
+
## Building
|
|
33
|
+
|
|
34
|
+
To build your library:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm run package
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
To create a production version of your showcase app:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm run build
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can preview the production build with `npm run preview`.
|
|
47
|
+
|
|
48
|
+
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
|
|
49
|
+
|
|
50
|
+
## Publishing
|
|
51
|
+
|
|
52
|
+
Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
|
|
53
|
+
|
|
54
|
+
To publish your library to [npm](https://www.npmjs.com):
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm publish
|
|
58
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as apiProxyHandle } from "./proxy/proxy.handle.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Handle, type RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
export type ProxyRequestInterceptorArgs = {
|
|
3
|
+
originalRequest: RequestEvent['request'];
|
|
4
|
+
cookies: RequestEvent['cookies'];
|
|
5
|
+
};
|
|
6
|
+
export type ProxyRequestInterceptor = (proxyRequest: Request, opt: ProxyRequestInterceptorArgs) => Promise<Request>;
|
|
7
|
+
export type ProxyResponseInterceptorArgs = {
|
|
8
|
+
originalRequest: RequestEvent['request'];
|
|
9
|
+
proxyRequest: Request;
|
|
10
|
+
};
|
|
11
|
+
export type ProxyResponseInterceptor = (proxyRequest: Response, opt: ProxyResponseInterceptorArgs) => Promise<Response>;
|
|
12
|
+
export type ProxyOptions = {
|
|
13
|
+
proxyPath: string;
|
|
14
|
+
proxyEndpoint: string;
|
|
15
|
+
requestInterceptors?: ProxyRequestInterceptor[];
|
|
16
|
+
responseInterceptors?: ProxyResponseInterceptor[];
|
|
17
|
+
setResponseHeaders?: Record<string, string> | ((request: Request, response: Response) => Record<string, string>);
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Taken from https://sami.website/blog/sveltekit-api-reverse-proxy
|
|
21
|
+
* @param param0
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
declare const apiProxyHandle: ({ proxyPath, proxyEndpoint, requestInterceptors, responseInterceptors, setResponseHeaders }: ProxyOptions) => Handle;
|
|
25
|
+
export default apiProxyHandle;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { dev } from '$app/environment';
|
|
2
|
+
import { error } from '@sveltejs/kit';
|
|
3
|
+
if (dev) {
|
|
4
|
+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Taken from https://sami.website/blog/sveltekit-api-reverse-proxy
|
|
8
|
+
* @param param0
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
const apiProxyHandle = ({ proxyPath, proxyEndpoint, requestInterceptors = [], responseInterceptors = [], setResponseHeaders }) => async ({ event, resolve }) => {
|
|
12
|
+
const { url, request, cookies } = event;
|
|
13
|
+
if (!url.pathname.startsWith(proxyPath)) {
|
|
14
|
+
return resolve(event);
|
|
15
|
+
}
|
|
16
|
+
const getProxyHeaders = typeof setResponseHeaders !== 'function'
|
|
17
|
+
? (() => setResponseHeaders || {})
|
|
18
|
+
: setResponseHeaders;
|
|
19
|
+
// console.log(
|
|
20
|
+
// `[PROXY] Begin ${request.method} proxy request from ${proxyPath} to ${proxyEndpoint}`
|
|
21
|
+
// );
|
|
22
|
+
// reject requests that don't come from the webapp, to avoid your proxy being abused.
|
|
23
|
+
// if (!origin || new URL(origin).origin !== url.origin) {
|
|
24
|
+
// throw error(403, "Request Forbidden.");
|
|
25
|
+
// }
|
|
26
|
+
const strippedPath = url.pathname.substring(proxyPath.length);
|
|
27
|
+
// build the new URL path with your API base URL, the stripped path and the query string
|
|
28
|
+
const urlPath = `${proxyEndpoint}${strippedPath}${event.url.search}`;
|
|
29
|
+
const proxiedUrl = new URL(urlPath);
|
|
30
|
+
// only forward these headers
|
|
31
|
+
// todo: Determine what other headers we shoud forward
|
|
32
|
+
const forwardReqHeaders = ['Authorization', 'Content-Type', 'Accept'].reduce((acc, k) => {
|
|
33
|
+
const header = event.request.headers.get(k);
|
|
34
|
+
if (header) {
|
|
35
|
+
// console.log(k, header);
|
|
36
|
+
acc.append(k, header);
|
|
37
|
+
}
|
|
38
|
+
return acc;
|
|
39
|
+
}, new Headers());
|
|
40
|
+
const proxyOpt = {
|
|
41
|
+
// propagate the request method and body
|
|
42
|
+
body: event.request.body,
|
|
43
|
+
method: event.request.method,
|
|
44
|
+
headers: forwardReqHeaders,
|
|
45
|
+
// headers: event.request.headers,
|
|
46
|
+
// headers: {
|
|
47
|
+
// cookie: event.request.headers.get('cookie') ?? ''
|
|
48
|
+
// },
|
|
49
|
+
credentials: 'include'
|
|
50
|
+
};
|
|
51
|
+
if (event.request.method === 'POST' ||
|
|
52
|
+
event.request.method === 'PATCH' ||
|
|
53
|
+
event.request.method === 'DELETE' ||
|
|
54
|
+
event.request.method === 'PUT') {
|
|
55
|
+
proxyOpt.duplex = 'half';
|
|
56
|
+
// // reading the body (debug)
|
|
57
|
+
// const reader = event.request.body!.getReader()
|
|
58
|
+
// let html = '', x = true;
|
|
59
|
+
// while (x) {
|
|
60
|
+
// const { value, done } = await reader.read()
|
|
61
|
+
// if (value) {
|
|
62
|
+
// html += new TextDecoder().decode(value)
|
|
63
|
+
// }
|
|
64
|
+
// if (done) {
|
|
65
|
+
// x = false;
|
|
66
|
+
// }
|
|
67
|
+
// }
|
|
68
|
+
// console.log('READING BODY', html)
|
|
69
|
+
}
|
|
70
|
+
const proxyReq = new Request(proxiedUrl, proxyOpt);
|
|
71
|
+
try {
|
|
72
|
+
let req = proxyReq;
|
|
73
|
+
for (let i = 0; i < requestInterceptors.length; i++) {
|
|
74
|
+
const interceptor = requestInterceptors[i];
|
|
75
|
+
req = await interceptor(req, {
|
|
76
|
+
originalRequest: request,
|
|
77
|
+
cookies
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
req.headers.set('Connection', 'close');
|
|
81
|
+
// console.log('BEFOIRE FETCH', proxiedUrl, proxyOpt, req.headers)
|
|
82
|
+
let response = await event.fetch(req);
|
|
83
|
+
// console.log(
|
|
84
|
+
// `[PROXY] Response from proxy ${req.url.toString()}. ${response.status} ${response.statusText}`
|
|
85
|
+
// );
|
|
86
|
+
for (let i = 0; i < responseInterceptors.length; i++) {
|
|
87
|
+
const interceptor = responseInterceptors[i];
|
|
88
|
+
response = await interceptor(response, {
|
|
89
|
+
originalRequest: request,
|
|
90
|
+
proxyRequest: req
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
// todo: check for the cookie and pass that down
|
|
94
|
+
const passHeaders = ['cache-control', 'vary', 'content-type'].reduce((acc, k) => {
|
|
95
|
+
const header = response.headers.get(k);
|
|
96
|
+
if (header) {
|
|
97
|
+
acc.append(k, header);
|
|
98
|
+
}
|
|
99
|
+
return acc;
|
|
100
|
+
}, new Headers());
|
|
101
|
+
const contentType = response.headers.get('content-type');
|
|
102
|
+
const responseOptions = {
|
|
103
|
+
headers: passHeaders,
|
|
104
|
+
status: response.status,
|
|
105
|
+
statusText: response.statusText
|
|
106
|
+
};
|
|
107
|
+
// apply overwritten header values to the proxied response
|
|
108
|
+
const proxyHeaders = getProxyHeaders(request, response);
|
|
109
|
+
for (const k in proxyHeaders) {
|
|
110
|
+
const header = proxyHeaders[k];
|
|
111
|
+
responseOptions.headers.set(k, header);
|
|
112
|
+
}
|
|
113
|
+
if (response.status === 204) {
|
|
114
|
+
return Response.json({}, {
|
|
115
|
+
...responseOptions,
|
|
116
|
+
status: 200
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
// console.log('test', contentType?.startsWith('text/'))
|
|
120
|
+
if (contentType?.startsWith('text/')) {
|
|
121
|
+
return new Response(await response.text(), responseOptions);
|
|
122
|
+
}
|
|
123
|
+
const data = await response.json();
|
|
124
|
+
return Response.json(data, responseOptions);
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
console.error(`Could not proxy API request to ${proxiedUrl.toString()}`);
|
|
128
|
+
console.log(e);
|
|
129
|
+
error(500, e instanceof Error ? { message: e.message } : { message: 'Unknown error has occurred' });
|
|
130
|
+
}
|
|
131
|
+
// should never reach here
|
|
132
|
+
return resolve(event);
|
|
133
|
+
};
|
|
134
|
+
export default apiProxyHandle;
|