-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(astro): Automatically add Sentry middleware in Astro integration (…
…#9532) This change adds automatic registration of our Astro middleware. This is possible since Astro 3.5.2 by [adding middleware](https://docs.astro.build/en/reference/integrations-reference/#addmiddleware-option) entry points in the astro integration's setup hook. This is backwards compatible with previous Astro versions because we can simply check if the `addMiddleware` function exists and only make use of it if it does.
- Loading branch information
Showing
11 changed files
with
507 additions
and
83 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
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
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
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,16 @@ | ||
import type { MiddlewareResponseHandler } from 'astro'; | ||
|
||
import { handleRequest } from '../../server/middleware'; | ||
|
||
/** | ||
* This export is used by our integration to automatically add the middleware | ||
* to astro ^3.5.0 projects. | ||
* | ||
* It's not possible to pass options at this moment, so we'll call our middleware | ||
* factory function with the default options. Users can deactiveate the automatic | ||
* middleware registration in our integration and manually add it in their own | ||
* `/src/middleware.js` file. | ||
*/ | ||
export const onRequest: MiddlewareResponseHandler = (ctx, next) => { | ||
return handleRequest()(ctx, next); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { vi } from 'vitest'; | ||
|
||
import { onRequest } from '../../../src/integration/middleware'; | ||
|
||
vi.mock('../../../src/server/meta', () => ({ | ||
getTracingMetaTags: () => ({ | ||
sentryTrace: '<meta name="sentry-trace" content="123">', | ||
baggage: '<meta name="baggage" content="abc">', | ||
}), | ||
})); | ||
|
||
describe('Integration middleware', () => { | ||
it('exports an onRequest middleware request handler', async () => { | ||
expect(typeof onRequest).toBe('function'); | ||
|
||
const next = vi.fn().mockReturnValue(Promise.resolve(new Response(null, { status: 200, headers: new Headers() }))); | ||
const ctx = { | ||
request: { | ||
method: 'GET', | ||
url: '/users/123/details', | ||
headers: new Headers(), | ||
}, | ||
url: new URL('https://myDomain.io/users/123/details'), | ||
params: { | ||
id: '123', | ||
}, | ||
}; | ||
// @ts-expect-error - a partial ctx object is fine here | ||
const res = await onRequest(ctx, next); | ||
|
||
expect(res).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.