Skip to content

Commit ebc556d

Browse files
committed
chore(fastify): Remove @clerk/clerk-sdk-node dependency
1 parent 7142f3c commit ebc556d

11 files changed

Lines changed: 55 additions & 65 deletions

package-lock.json

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

packages/fastify/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"test": "jest"
2828
},
2929
"dependencies": {
30-
"@clerk/clerk-sdk-node": "^4.7.0-staging.1",
3130
"@clerk/types": "^3.26.0-staging.1",
3231
"@clerk/backend": "^0.6.0-staging.0",
3332
"cookies": "0.8.0"

packages/fastify/src/__snapshots__/clerk.test.ts.snap

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const ClerkMock = jest.fn(() => {
22
return 'clerkClient';
33
});
4-
jest.mock('@clerk/clerk-sdk-node', () => {
5-
return { Clerk: ClerkMock };
4+
5+
jest.mock('@clerk/backend', () => {
6+
return {
7+
...jest.requireActual('@clerk/backend'),
8+
Clerk: ClerkMock,
9+
};
610
});
711

8-
import { clerkClient } from './clerk';
12+
import { clerkClient } from './clerkClient';
913

1014
describe('clerk', () => {
1115
afterAll(() => {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Clerk } from '@clerk/clerk-sdk-node';
1+
import { Clerk } from '@clerk/backend';
22

33
import { API_URL, API_VERSION, JWT_KEY, SECRET_KEY } from './constants';
44

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ClerkOptions } from '@clerk/types';
2+
import type { FastifyInstance, FastifyPluginCallback } from 'fastify';
3+
import fp from 'fastify-plugin';
4+
5+
import { polyfillServerResponseMethods } from './polyfill';
6+
import { withClerkMiddleware } from './withClerkMiddleware';
7+
8+
const plugin: FastifyPluginCallback = (instance: FastifyInstance, opts: ClerkOptions, done) => {
9+
polyfillServerResponseMethods(instance);
10+
instance.decorateRequest('auth', null);
11+
// run clerk as a middleware to all scoped routes
12+
instance.addHook('preHandler', withClerkMiddleware(opts));
13+
14+
done();
15+
};
16+
17+
export const clerkPlugin = fp(plugin, {
18+
name: '@clerk/fastify',
19+
fastify: '4.x',
20+
});

packages/fastify/src/getAuth.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { AuthObject } from '@clerk/backend';
2+
import type { FastifyRequest } from 'fastify';
3+
4+
import { pluginRegistrationRequired } from './errors';
5+
6+
type FastifyRequestWithAuth = FastifyRequest & { auth: AuthObject };
7+
8+
export const getAuth = (req: FastifyRequest): AuthObject => {
9+
const authReq = req as FastifyRequestWithAuth;
10+
11+
if (!authReq.auth) {
12+
throw new Error(pluginRegistrationRequired);
13+
}
14+
15+
return authReq.auth;
16+
};

packages/fastify/src/index.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ describe('@clerk/fastify', () => {
5252
expect(api.getAuth(req)).toEqual('authObj');
5353
});
5454

55-
test('returns {} when req.auth as default', () => {
56-
const req = { key1: 'asa', auth: null } as any as FastifyRequest;
57-
58-
expect(api.getAuth(req)).toEqual({});
59-
});
60-
6155
test('throws error if clerkPlugin is on registered', () => {
6256
const req = { key1: 'asa' } as any as FastifyRequest;
6357

packages/fastify/src/index.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,2 @@
1-
import type { WithAuthProp } from '@clerk/clerk-sdk-node';
2-
import type { ClerkOptions } from '@clerk/types';
3-
import type { FastifyInstance, FastifyPluginCallback, FastifyRequest } from 'fastify';
4-
import fp from 'fastify-plugin';
5-
6-
import { pluginRegistrationRequired } from './errors';
7-
import { polyfillServerResponseMethods } from './polyfill';
8-
import { withClerkMiddleware } from './withClerkMiddleware';
9-
10-
const plugin: FastifyPluginCallback = (instance: FastifyInstance, opts: ClerkOptions, done) => {
11-
polyfillServerResponseMethods(instance);
12-
instance.decorateRequest('auth', null);
13-
// run clerk as a middleware to all scoped routes
14-
instance.addHook('preHandler', withClerkMiddleware(opts));
15-
16-
done();
17-
};
18-
19-
export const clerkPlugin = fp(plugin, {
20-
name: '@clerk/fastify',
21-
fastify: '4.x',
22-
});
23-
24-
export const getAuth = (req: FastifyRequest) => {
25-
const authReq = req as WithAuthProp<FastifyRequest>;
26-
27-
if (authReq.auth === undefined) {
28-
throw new Error(pluginRegistrationRequired);
29-
}
30-
31-
return authReq.auth || {};
32-
};
1+
export { clerkPlugin } from './clerkPlugin';
2+
export { getAuth } from './getAuth';

packages/fastify/src/withClerkMiddleware.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import type { FastifyReply, FastifyRequest } from 'fastify';
22
import Fastify from 'fastify';
33

4+
import { clerkPlugin, getAuth } from './index';
5+
46
const authenticateRequestMock = jest.fn();
57
const localInterstitialMock = jest.fn();
6-
jest.mock('@clerk/clerk-sdk-node', () => {
8+
9+
jest.mock('@clerk/backend', () => {
710
return {
11+
...jest.requireActual('@clerk/backend'),
812
Clerk: () => {
913
return {
10-
authenticateRequest: authenticateRequestMock,
11-
localInterstitial: localInterstitialMock,
14+
authenticateRequest: (...args: any) => authenticateRequestMock(...args),
15+
localInterstitial: (...args: any) => localInterstitialMock(...args),
1216
};
1317
},
1418
};
1519
});
1620

17-
import { clerkPlugin, getAuth } from './index';
18-
1921
describe('withClerkMiddleware(options)', () => {
2022
beforeEach(() => {
2123
jest.clearAllMocks();

0 commit comments

Comments
 (0)