Skip to content

Commit

Permalink
fix: do not rewrite /api and /data-api requests to SvelteKit (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrich authored Nov 20, 2023
1 parent 0fe3eaa commit aa36771
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
coverage/
*.tgz
.vscode/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
CHANGELOG.md
coverage/
build/
.svelte-kit/
sk_render/
1 change: 1 addition & 0 deletions demo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ node_modules
.output
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
sk_render/
2 changes: 2 additions & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions demo/func/HelloWorld/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get", "post"]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
13 changes: 13 additions & 0 deletions demo/func/HelloWorld/index.js
Original file line number Diff line number Diff line change
@@ -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
};
};
3 changes: 3 additions & 0 deletions demo/func/HelloWorld/sample.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Azure"
}
7 changes: 7 additions & 0 deletions demo/func/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}
1 change: 1 addition & 0 deletions demo/func/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 3 additions & 1 deletion demo/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
}
};

Expand Down
9 changes: 9 additions & 0 deletions demo/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ export function generateConfig(customStaticWebAppConfig, appDir) {
...customStaticWebAppConfig,
routes: [
...customStaticWebAppConfig.routes,
{
route: '/api/*'
},
{
route: '/data-api/*'
},
{
route: '*',
methods: ['POST', 'PUT', 'DELETE'],
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('generateConfig', () => {
platform: {
apiRuntime: 'node:16'
},
routes: [
routes: expect.arrayContaining([
{
methods: ['POST', 'PUT', 'DELETE'],
rewrite: '/api/__render',
Expand All @@ -38,7 +38,7 @@ describe('generateConfig', () => {
},
route: '/appDir/immutable/*'
}
]
])
});
});

Expand Down

0 comments on commit aa36771

Please sign in to comment.