From aa3677133d404cf7bb396ea3f4c41ea026598ce7 Mon Sep 17 00:00:00 2001 From: Geoff Rich <4992896+geoffrich@users.noreply.github.com> Date: Sun, 19 Nov 2023 16:40:56 -0800 Subject: [PATCH] fix: do not rewrite /api and /data-api requests to SvelteKit (#162) --- ...e-static-web-apps-polite-desert-00b80111e.yml | 2 +- .gitignore | 1 + .prettierignore | 3 +++ demo/.gitignore | 1 + demo/README.md | 2 ++ demo/func/HelloWorld/function.json | 16 ++++++++++++++++ demo/func/HelloWorld/index.js | 13 +++++++++++++ demo/func/HelloWorld/sample.dat | 3 +++ demo/func/host.json | 7 +++++++ demo/func/package.json | 1 + demo/svelte.config.js | 4 +++- demo/tests/test.js | 9 +++++++++ index.js | 6 ++++++ test/index.test.js | 4 ++-- 14 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 demo/func/HelloWorld/function.json create mode 100644 demo/func/HelloWorld/index.js create mode 100644 demo/func/HelloWorld/sample.dat create mode 100644 demo/func/host.json create mode 100644 demo/func/package.json diff --git a/.github/workflows/azure-static-web-apps-polite-desert-00b80111e.yml b/.github/workflows/azure-static-web-apps-polite-desert-00b80111e.yml index 4f18a63..e4e4b22 100644 --- a/.github/workflows/azure-static-web-apps-polite-desert-00b80111e.yml +++ b/.github/workflows/azure-static-web-apps-polite-desert-00b80111e.yml @@ -44,7 +44,7 @@ jobs: ###### Repository/Build Configurations - These values can be configured to match your app requirements. ###### # For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig app_location: '/demo' # App source code path - api_location: 'demo/build/server' # Api source code path - optional + api_location: 'demo/func' # Api source code path - optional output_location: 'build/static' # Built app content directory - optional # needed when we set CUSTOM_BUILD_COMMAND skip_api_build: true diff --git a/.gitignore b/.gitignore index ddedbd1..396c0ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ coverage/ *.tgz +.vscode/ \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index 50eec90..06fcbaf 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,5 @@ CHANGELOG.md coverage/ +build/ +.svelte-kit/ +sk_render/ \ No newline at end of file diff --git a/demo/.gitignore b/demo/.gitignore index 8f6c617..300d87a 100644 --- a/demo/.gitignore +++ b/demo/.gitignore @@ -10,3 +10,4 @@ node_modules .output vite.config.js.timestamp-* vite.config.ts.timestamp-* +sk_render/ \ No newline at end of file diff --git a/demo/README.md b/demo/README.md index aa1ede8..aa165af 100644 --- a/demo/README.md +++ b/demo/README.md @@ -4,6 +4,8 @@ This is a repo demonstrating how to use [svelte-adapter-azure-swa](https://www.n This demo uses the local version of the adapter to make testing unreleased changes easier. In your app, you should install `svelte-adapter-azure-swa` from npm. +This demo also uses a custom Azure function to make testing that integration easier. If you do not need a custom Azure function, you do not need the `func/` folder or need to set the `apiDir` option in `svelte.config.js`. + [Deployed demo](https://polite-desert-00b80111e.2.azurestaticapps.net/) ## Developing diff --git a/demo/func/HelloWorld/function.json b/demo/func/HelloWorld/function.json new file mode 100644 index 0000000..1c1f040 --- /dev/null +++ b/demo/func/HelloWorld/function.json @@ -0,0 +1,16 @@ +{ + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": ["get", "post"] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/demo/func/HelloWorld/index.js b/demo/func/HelloWorld/index.js new file mode 100644 index 0000000..bd41adb --- /dev/null +++ b/demo/func/HelloWorld/index.js @@ -0,0 +1,13 @@ +module.exports = async function (context, req) { + context.log('JavaScript HTTP trigger function processed a request.'); + + const name = req.query.name || (req.body && req.body.name); + const responseMessage = name + ? 'Hello, ' + name + '. This HTTP triggered function executed successfully.' + : 'This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.'; + + context.res = { + // status: 200, /* Defaults to 200 */ + body: responseMessage + }; +}; diff --git a/demo/func/HelloWorld/sample.dat b/demo/func/HelloWorld/sample.dat new file mode 100644 index 0000000..2e60943 --- /dev/null +++ b/demo/func/HelloWorld/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/demo/func/host.json b/demo/func/host.json new file mode 100644 index 0000000..14992fc --- /dev/null +++ b/demo/func/host.json @@ -0,0 +1,7 @@ +{ + "version": "2.0", + "extensionBundle": { + "id": "Microsoft.Azure.Functions.ExtensionBundle", + "version": "[2.*, 3.0.0)" + } +} diff --git a/demo/func/package.json b/demo/func/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/demo/func/package.json @@ -0,0 +1 @@ +{} diff --git a/demo/svelte.config.js b/demo/svelte.config.js index 3d40902..01d4b4f 100644 --- a/demo/svelte.config.js +++ b/demo/svelte.config.js @@ -3,7 +3,9 @@ import adapter from 'svelte-adapter-azure-swa'; /** @type {import('@sveltejs/kit').Config} */ const config = { kit: { - adapter: adapter() + adapter: adapter({ + apiDir: './func' + }) } }; diff --git a/demo/tests/test.js b/demo/tests/test.js index f19a1eb..ec6c58a 100644 --- a/demo/tests/test.js +++ b/demo/tests/test.js @@ -17,3 +17,12 @@ test('submits sverdle guess', async ({ page }) => { await expect(input).toHaveValue('a'); await expect(input).toBeDisabled(); }); + +test('can call custom API azure function', async ({ request }) => { + const response = await request.post('/api/HelloWorld', { + data: { + name: 'Geoff' + } + }); + expect(response.ok()).toBeTruthy(); +}); diff --git a/index.js b/index.js index d984241..829d9aa 100644 --- a/index.js +++ b/index.js @@ -196,6 +196,12 @@ export function generateConfig(customStaticWebAppConfig, appDir) { ...customStaticWebAppConfig, routes: [ ...customStaticWebAppConfig.routes, + { + route: '/api/*' + }, + { + route: '/data-api/*' + }, { route: '*', methods: ['POST', 'PUT', 'DELETE'], diff --git a/test/index.test.js b/test/index.test.js index 9711b1a..70b9776 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -26,7 +26,7 @@ describe('generateConfig', () => { platform: { apiRuntime: 'node:16' }, - routes: [ + routes: expect.arrayContaining([ { methods: ['POST', 'PUT', 'DELETE'], rewrite: '/api/__render', @@ -38,7 +38,7 @@ describe('generateConfig', () => { }, route: '/appDir/immutable/*' } - ] + ]) }); });