-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
package & sveltekit-zero-api preflight requests
- Loading branch information
Showing
7 changed files
with
94 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.DS_Store | ||
node_modules | ||
/build | ||
/.svelte-kit | ||
.env | ||
.env.* | ||
!.env.example | ||
vite.config.js.timestamp-* | ||
vite.config.ts.timestamp-* | ||
|
||
# Local Netlify folder | ||
.netlify | ||
dist | ||
|
||
# npm | ||
src | ||
.netlify | ||
.svelte-kit | ||
.git | ||
.vscode | ||
patches | ||
static | ||
tests | ||
|
||
.eslintignore | ||
.eslintrc.cjs | ||
.npmrc | ||
.prettierignore | ||
.prettierrc | ||
*.config.* | ||
tsconfig.json | ||
netlify* | ||
*-lock* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
import type { Handle } from '@sveltejs/kit' | ||
|
||
const allowAnyOrigin = { 'Access-Control-Allow-Origin': '*' } | ||
export const handle: Handle = async ({ event, resolve }) => { | ||
if (event.request.method === 'GET') { | ||
event.setHeaders({ | ||
'Access-Control-Allow-Origin': '*', | ||
if (event.request.method === 'OPTIONS') { | ||
// Respond to preflight requests | ||
// I'm assuming that sveltekit internal fetch()/Requests | ||
// comply with these headers | ||
// but to allow sveltekit-zero-api's handler function to do it's job it must be explicit | ||
return new Response(null, { | ||
headers: { | ||
...allowAnyOrigin, | ||
'Access-Control-Allow-Methods': 'GET', | ||
'Access-Control-Allow-Headers': | ||
'Content-Type, cache-control, x-requested-with', | ||
}, | ||
}) | ||
} | ||
|
||
if (event.request.method === 'GET') { | ||
event.setHeaders(allowAnyOrigin) | ||
} | ||
|
||
return await resolve(event) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
import Api from './api' | ||
import { createZeroApi } from 'sveltekit-zero-api/api' | ||
import type { GeneratedAPI } from './sveltekit-zero-api' | ||
|
||
export default Api | ||
const routes = createZeroApi({ | ||
baseUrl: 'https://youtube-browser-api.netlify.app', | ||
onError: async err => console.error('[youtube-browser-api]', err), | ||
prependCallbacks(method) { | ||
method.Error(err => { | ||
console.warn('Error', err) | ||
}) | ||
}, | ||
}) as GeneratedAPI | ||
|
||
export default routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters