Skip to content

Commit 61c374d

Browse files
biltongzageoffrich
andauthored
feat: support SvelteKit 1.0.0-next.234 (#29)
* update adapter to support sveltekit 1.0.0-next.234 * Refactor request/response creation * Missing await Co-authored-by: Geoff Rich <[email protected]>
1 parent 70702ff commit 61c374d

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

files/entry.js

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,62 @@ const app = new App(manifest);
2020
* @param {Context} context
2121
*/
2222
export async function index(context) {
23+
const request = toRequest(context);
24+
25+
if (debug) {
26+
context.log(`Request: ${JSON.stringify(request)}`);
27+
}
28+
29+
const rendered = await app.render(request);
30+
const response = await toResponse(rendered);
31+
32+
if (debug) {
33+
context.log(`Response: ${JSON.stringify(response)}`);
34+
}
35+
36+
context.res = response;
37+
}
38+
39+
/**
40+
* @param {Context} context
41+
* @returns {Request}
42+
* */
43+
function toRequest(context) {
2344
const { method, headers, rawBody: body } = context.req;
2445
// because we proxy all requests to the render function, the original URL in the request is /api/__render
2546
// this header contains the URL the user requested
2647
const originalUrl = headers['x-ms-original-url'];
27-
const url = new URL(originalUrl);
2848

29-
const rawBody = typeof body === 'string' ? Buffer.from(body, 'utf-8') : body;
30-
const request = {
31-
url,
49+
/** @type {RequestInit} */
50+
const init = {
3251
method,
33-
headers,
34-
rawBody
52+
headers: new Headers(headers)
3553
};
3654

37-
if (debug) {
38-
context.log(`Request: ${JSON.stringify(request)}`);
55+
if (method !== 'GET' && method !== 'HEAD') {
56+
init.body = typeof body === 'string' ? Buffer.from(body, 'utf-8') : body;
3957
}
4058

41-
const rendered = await app.render(request);
59+
return new Request(originalUrl, init);
60+
}
61+
62+
/**
63+
* @param {Response} rendered
64+
* @returns {Promise<Record<string, any>>}
65+
*/
66+
async function toResponse(rendered) {
67+
const { status } = rendered;
68+
const resBody = await rendered.text();
4269

43-
const { status, headers: resHeaders, body: resBody } = rendered;
70+
/** @type {Record<string, string>} */
71+
const resHeaders = {};
72+
rendered.headers.forEach((value, key) => {
73+
resHeaders[key] = value;
74+
});
4475

45-
const response = {
76+
return {
4677
status,
4778
body: resBody,
48-
headers: resHeaders,
49-
rawBody
79+
headers: resHeaders
5080
};
51-
52-
if (debug) {
53-
context.log(`Response: ${JSON.stringify(response)}`);
54-
}
55-
56-
context.res = response;
5781
}

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
"url": "https://github.com/geoffrich/svelte-adapter-azure-swa/issues"
2828
},
2929
"peerDependencies": {
30-
"@sveltejs/kit": "^1.0.0-next.218"
30+
"@sveltejs/kit": "^1.0.0-next.234"
3131
},
3232
"devDependencies": {
3333
"@azure/functions": "^1.2.3",
34-
"@sveltejs/kit": "^1.0.0-next.218",
34+
"@sveltejs/kit": "^1.0.0-next.234",
3535
"@types/node": "^17.0.5",
3636
"prettier": "^2.4.1"
3737
},

0 commit comments

Comments
 (0)