@logto/next 3.3.4 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,6 +47,8 @@ const getLogtoContext = async (config, getContextParameters) => {
47
47
  };
48
48
  /**
49
49
  * Get organization tokens from session
50
+ *
51
+ * @deprecated Use getOrganizationToken instead
50
52
  */
51
53
  const getOrganizationTokens = async (config) => {
52
54
  const { isAuthenticated } = await getLogtoContext(config);
@@ -61,9 +63,55 @@ const getOrganizationTokens = async (config) => {
61
63
  token: await nodeClient.getOrganizationToken(organizationId),
62
64
  })));
63
65
  };
66
+ /**
67
+ * Get access token for the specified resource or organization,
68
+ * this function can be used in server actions or API routes
69
+ */
70
+ const getAccessToken = async (config, resource, organizationId) => {
71
+ const client$1 = new client.default(config);
72
+ const { nodeClient, session } = await client$1.createNodeClientFromHeaders(await cookie.getCookies(config));
73
+ const accessToken = await nodeClient.getAccessToken(resource, organizationId);
74
+ // Update access token cache
75
+ const newCookie = await session.getValues?.();
76
+ if (newCookie) {
77
+ await cookie.setCookies(newCookie, config);
78
+ }
79
+ return accessToken;
80
+ };
81
+ /**
82
+ * Get organization token from session,
83
+ * this function can be used in server actions or API routes
84
+ */
85
+ const getOrganizationToken = async (config, organizationId) => {
86
+ return getAccessToken(config, undefined, organizationId);
87
+ };
88
+ /**
89
+ * Get access token for the specified resource or organization,
90
+ * this function can be used in React Server Components (RSC)
91
+ * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
92
+ * When using server actions or API routes, we highly recommand to use the getAccessToken method
93
+ */
94
+ const getAccessTokenRSC = async (config, resource, organizationId) => {
95
+ const client$1 = new client.default(config);
96
+ const { nodeClient } = await client$1.createNodeClientFromHeaders(await cookie.getCookies(config));
97
+ return nodeClient.getAccessToken(resource, organizationId);
98
+ };
99
+ /**
100
+ * Get organization token from session,
101
+ * this function can be used in React Server Components (RSC)
102
+ * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
103
+ * When using server actions or API routes, we highly recommand to use the getOrganizationToken method
104
+ */
105
+ const getOrganizationTokenRSC = async (config, organizationId) => {
106
+ return getAccessTokenRSC(config, undefined, organizationId);
107
+ };
64
108
 
65
109
  exports.default = client.default;
110
+ exports.getAccessToken = getAccessToken;
111
+ exports.getAccessTokenRSC = getAccessTokenRSC;
66
112
  exports.getLogtoContext = getLogtoContext;
113
+ exports.getOrganizationToken = getOrganizationToken;
114
+ exports.getOrganizationTokenRSC = getOrganizationTokenRSC;
67
115
  exports.getOrganizationTokens = getOrganizationTokens;
68
116
  exports.handleSignIn = handleSignIn;
69
117
  exports.signIn = signIn;
@@ -14,12 +14,47 @@ export declare const signOut: (config: LogtoNextConfig, redirectUri?: string) =>
14
14
  /**
15
15
  * Get Logto context from session, including auth status and claims
16
16
  */
17
- export declare const getLogtoContext: (config: LogtoNextConfig, getContextParameters?: GetContextParameters) => Promise<LogtoContext>;
17
+ export declare const getLogtoContext: (config: LogtoNextConfig, getContextParameters?: Omit<GetContextParameters, 'getAccessToken' | 'resource' | 'organizationId' | 'getOrganizationToken'> & {
18
+ /** @deprecated use getAccessTokenRSC() */
19
+ getAccessToken?: GetContextParameters['getAccessToken'];
20
+ /** @deprecated use getOrganizationTokenRSC() */
21
+ getOrganizationToken?: GetContextParameters['getOrganizationToken'];
22
+ /** @deprecated use getAccessTokenRSC() */
23
+ resource?: GetContextParameters['resource'];
24
+ /** @deprecated use getOrganizationTokenRSC() */
25
+ organizationId?: GetContextParameters['organizationId'];
26
+ }) => Promise<LogtoContext>;
18
27
  /**
19
28
  * Get organization tokens from session
29
+ *
30
+ * @deprecated Use getOrganizationToken instead
20
31
  */
21
32
  export declare const getOrganizationTokens: (config: LogtoNextConfig) => Promise<{
22
33
  id: string;
23
34
  token: string;
24
35
  }[]>;
36
+ /**
37
+ * Get access token for the specified resource or organization,
38
+ * this function can be used in server actions or API routes
39
+ */
40
+ export declare const getAccessToken: (config: LogtoNextConfig, resource?: string, organizationId?: string) => Promise<string>;
41
+ /**
42
+ * Get organization token from session,
43
+ * this function can be used in server actions or API routes
44
+ */
45
+ export declare const getOrganizationToken: (config: LogtoNextConfig, organizationId?: string) => Promise<string>;
46
+ /**
47
+ * Get access token for the specified resource or organization,
48
+ * this function can be used in React Server Components (RSC)
49
+ * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
50
+ * When using server actions or API routes, we highly recommand to use the getAccessToken method
51
+ */
52
+ export declare const getAccessTokenRSC: (config: LogtoNextConfig, resource?: string, organizationId?: string) => Promise<string>;
53
+ /**
54
+ * Get organization token from session,
55
+ * this function can be used in React Server Components (RSC)
56
+ * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
57
+ * When using server actions or API routes, we highly recommand to use the getOrganizationToken method
58
+ */
59
+ export declare const getOrganizationTokenRSC: (config: LogtoNextConfig, organizationId?: string) => Promise<string>;
25
60
  export { default } from './client';
@@ -43,6 +43,8 @@ const getLogtoContext = async (config, getContextParameters) => {
43
43
  };
44
44
  /**
45
45
  * Get organization tokens from session
46
+ *
47
+ * @deprecated Use getOrganizationToken instead
46
48
  */
47
49
  const getOrganizationTokens = async (config) => {
48
50
  const { isAuthenticated } = await getLogtoContext(config);
@@ -57,5 +59,47 @@ const getOrganizationTokens = async (config) => {
57
59
  token: await nodeClient.getOrganizationToken(organizationId),
58
60
  })));
59
61
  };
62
+ /**
63
+ * Get access token for the specified resource or organization,
64
+ * this function can be used in server actions or API routes
65
+ */
66
+ const getAccessToken = async (config, resource, organizationId) => {
67
+ const client = new LogtoClient(config);
68
+ const { nodeClient, session } = await client.createNodeClientFromHeaders(await getCookies(config));
69
+ const accessToken = await nodeClient.getAccessToken(resource, organizationId);
70
+ // Update access token cache
71
+ const newCookie = await session.getValues?.();
72
+ if (newCookie) {
73
+ await setCookies(newCookie, config);
74
+ }
75
+ return accessToken;
76
+ };
77
+ /**
78
+ * Get organization token from session,
79
+ * this function can be used in server actions or API routes
80
+ */
81
+ const getOrganizationToken = async (config, organizationId) => {
82
+ return getAccessToken(config, undefined, organizationId);
83
+ };
84
+ /**
85
+ * Get access token for the specified resource or organization,
86
+ * this function can be used in React Server Components (RSC)
87
+ * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
88
+ * When using server actions or API routes, we highly recommand to use the getAccessToken method
89
+ */
90
+ const getAccessTokenRSC = async (config, resource, organizationId) => {
91
+ const client = new LogtoClient(config);
92
+ const { nodeClient } = await client.createNodeClientFromHeaders(await getCookies(config));
93
+ return nodeClient.getAccessToken(resource, organizationId);
94
+ };
95
+ /**
96
+ * Get organization token from session,
97
+ * this function can be used in React Server Components (RSC)
98
+ * Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
99
+ * When using server actions or API routes, we highly recommand to use the getOrganizationToken method
100
+ */
101
+ const getOrganizationTokenRSC = async (config, organizationId) => {
102
+ return getAccessTokenRSC(config, undefined, organizationId);
103
+ };
60
104
 
61
- export { LogtoClient as default, getLogtoContext, getOrganizationTokens, handleSignIn, signIn, signOut };
105
+ export { LogtoClient as default, getAccessToken, getAccessTokenRSC, getLogtoContext, getOrganizationToken, getOrganizationTokenRSC, getOrganizationTokens, handleSignIn, signIn, signOut };
package/lib/src/index.cjs CHANGED
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var NodeClient = require('@logto/node');
6
6
  var client = require('./client.cjs');
7
+ var utils = require('./utils.cjs');
7
8
 
8
9
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
10
 
@@ -14,23 +15,23 @@ class LogtoClient extends client.default {
14
15
  super(config, {
15
16
  NodeClient: NodeClient__default.default,
16
17
  });
17
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => async (request, response) => {
18
+ this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode, onError) => utils.buildHandler(async (request, response) => {
18
19
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
19
20
  await nodeClient.signIn(redirectUri, interactionMode);
20
21
  await this.storage?.save();
21
22
  if (this.navigateUrl) {
22
23
  response.redirect(this.navigateUrl);
23
24
  }
24
- };
25
- this.handleSignInCallback = (redirectTo = this.config.baseUrl) => async (request, response) => {
25
+ }, onError);
26
+ this.handleSignInCallback = (redirectTo = this.config.baseUrl, onError) => utils.buildHandler(async (request, response) => {
26
27
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
27
28
  if (request.url) {
28
29
  await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
29
30
  await this.storage?.save();
30
31
  response.redirect(redirectTo);
31
32
  }
32
- };
33
- this.handleSignOut = (redirectUri = this.config.baseUrl) => async (request, response) => {
33
+ }, onError);
34
+ this.handleSignOut = (redirectUri = this.config.baseUrl, onError) => utils.buildHandler(async (request, response) => {
34
35
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
35
36
  await nodeClient.signOut(redirectUri);
36
37
  await this.storage?.destroy();
@@ -38,68 +39,41 @@ class LogtoClient extends client.default {
38
39
  if (this.navigateUrl) {
39
40
  response.redirect(this.navigateUrl);
40
41
  }
41
- };
42
- this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
42
+ }, onError);
43
+ this.handleUser = (configs, onError) => this.withLogtoApiRoute((request, response) => {
43
44
  response.json(request.user);
44
- }, configs);
45
+ }, configs, onError);
45
46
  this.handleAuthRoutes = (configs, onError) => (request, response) => {
46
- try {
47
- const { action } = request.query;
48
- if (action === 'sign-in') {
49
- return this.handleSignIn()(request, response);
50
- }
51
- if (action === 'sign-up') {
52
- return this.handleSignIn(undefined, 'signUp')(request, response);
53
- }
54
- if (action === 'sign-in-callback') {
55
- return this.handleSignInCallback()(request, response);
56
- }
57
- if (action === 'sign-out') {
58
- return this.handleSignOut()(request, response);
59
- }
60
- if (action === 'user') {
61
- return this.handleUser(configs)(request, response);
62
- }
63
- response.status(404).end();
47
+ const { action } = request.query;
48
+ if (action === 'sign-in') {
49
+ return this.handleSignIn(undefined, undefined, onError)(request, response);
64
50
  }
65
- catch (error) {
66
- if (onError) {
67
- return onError(request, response, error);
68
- }
69
- throw error;
51
+ if (action === 'sign-up') {
52
+ return this.handleSignIn(undefined, 'signUp', onError)(request, response);
70
53
  }
71
- };
72
- this.withLogtoApiRoute = (handler, config = {}, onError) => async (request, response) => {
73
- try {
74
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
75
- const user = await nodeClient.getContext(config);
76
- await this.storage?.save();
77
- // eslint-disable-next-line @silverhand/fp/no-mutating-methods
78
- Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
79
- return handler(request, response);
54
+ if (action === 'sign-in-callback') {
55
+ return this.handleSignInCallback(undefined, onError)(request, response);
56
+ }
57
+ if (action === 'sign-out') {
58
+ return this.handleSignOut(undefined, onError)(request, response);
80
59
  }
81
- catch (error) {
82
- if (onError) {
83
- return onError(request, response, error);
84
- }
85
- throw error;
60
+ if (action === 'user') {
61
+ return this.handleUser(configs)(request, response);
86
62
  }
63
+ response.status(404).end();
87
64
  };
65
+ this.withLogtoApiRoute = (handler, config = {}, onError) => utils.buildHandler(async (request, response) => {
66
+ const nodeClient = await this.createNodeClientFromNextApi(request, response);
67
+ const user = await nodeClient.getContext(config);
68
+ await this.storage?.save();
69
+ // eslint-disable-next-line @silverhand/fp/no-mutating-methods
70
+ Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
71
+ return handler(request, response);
72
+ }, onError);
88
73
  this.withLogtoSsr = (handler, configs = {}, onError) => async (context) => {
89
- try {
90
- const nodeClient = await this.createNodeClientFromNextApi(context.req, context.res);
91
- const user = await nodeClient.getContext(configs);
92
- await this.storage?.save();
93
- // eslint-disable-next-line @silverhand/fp/no-mutating-methods
94
- Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
95
- return await handler(context);
96
- }
97
- catch (error) {
98
- if (onError) {
99
- return onError(error);
100
- }
101
- throw error;
102
- }
74
+ return this.withLogtoApiRoute(async () => {
75
+ return handler(context);
76
+ }, configs, onError);
103
77
  };
104
78
  }
105
79
  async createNodeClientFromNextApi(request, response) {
@@ -1,22 +1,22 @@
1
1
  /// <reference types="node" />
2
2
  import { type IncomingMessage, type ServerResponse } from 'node:http';
3
3
  import NodeClient, { type GetContextParameters, type InteractionMode } from '@logto/node';
4
- import { type GetServerSidePropsResult, type GetServerSidePropsContext, type NextApiHandler, type NextApiRequest, type NextApiResponse } from 'next';
4
+ import { type GetServerSidePropsResult, type GetServerSidePropsContext, type NextApiHandler } from 'next';
5
5
  import { type NextApiRequestCookies } from 'next/dist/server/api-utils/index.js';
6
6
  import LogtoNextBaseClient from './client.js';
7
- import type { LogtoNextConfig } from './types.js';
7
+ import type { ErrorHandler, LogtoNextConfig } from './types.js';
8
8
  export type { LogtoNextConfig } from './types.js';
9
9
  export { LogtoError, LogtoRequestError, LogtoClientError, OidcError, Prompt, ReservedScope, ReservedResource, UserScope, organizationUrnPrefix, buildOrganizationUrn, getOrganizationIdFromUrn, PersistKey, } from '@logto/node';
10
10
  export type { AccessTokenClaims, IdTokenClaims, LogtoContext, InteractionMode, LogtoErrorCode, UserInfoResponse, } from '@logto/node';
11
11
  export default class LogtoClient extends LogtoNextBaseClient {
12
12
  constructor(config: LogtoNextConfig);
13
- handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode) => NextApiHandler;
14
- handleSignInCallback: (redirectTo?: string) => NextApiHandler;
15
- handleSignOut: (redirectUri?: string) => NextApiHandler;
16
- handleUser: (configs?: GetContextParameters) => NextApiHandler;
17
- handleAuthRoutes: (configs?: GetContextParameters, onError?: ((request: NextApiRequest, response: NextApiResponse, error: unknown) => unknown) | undefined) => NextApiHandler;
18
- withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters, onError?: ((request: NextApiRequest, response: NextApiResponse, error: unknown) => unknown) | undefined) => NextApiHandler;
19
- withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters, onError?: ((error: unknown) => unknown) | undefined) => (context: GetServerSidePropsContext) => Promise<unknown>;
13
+ handleSignIn: (redirectUri?: string, interactionMode?: InteractionMode, onError?: ErrorHandler) => NextApiHandler;
14
+ handleSignInCallback: (redirectTo?: string, onError?: ErrorHandler) => NextApiHandler;
15
+ handleSignOut: (redirectUri?: string, onError?: ErrorHandler) => NextApiHandler;
16
+ handleUser: (configs?: GetContextParameters, onError?: ErrorHandler) => NextApiHandler;
17
+ handleAuthRoutes: (configs?: GetContextParameters, onError?: ErrorHandler) => NextApiHandler;
18
+ withLogtoApiRoute: (handler: NextApiHandler, config?: GetContextParameters, onError?: ErrorHandler) => NextApiHandler;
19
+ withLogtoSsr: <P extends Record<string, unknown> = Record<string, unknown>>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, configs?: GetContextParameters, onError?: ((error: unknown) => unknown) | undefined) => (context: GetServerSidePropsContext) => Promise<NextApiHandler>;
20
20
  createNodeClientFromNextApi(request: IncomingMessage & {
21
21
  cookies: NextApiRequestCookies;
22
22
  }, response: ServerResponse): Promise<NodeClient>;
package/lib/src/index.js CHANGED
@@ -1,29 +1,30 @@
1
1
  import NodeClient, { createSession } from '@logto/node';
2
2
  export { LogtoClientError, LogtoError, LogtoRequestError, OidcError, PersistKey, Prompt, ReservedResource, ReservedScope, UserScope, buildOrganizationUrn, getOrganizationIdFromUrn, organizationUrnPrefix } from '@logto/node';
3
3
  import LogtoNextBaseClient from './client.js';
4
+ import { buildHandler } from './utils.js';
4
5
 
5
6
  class LogtoClient extends LogtoNextBaseClient {
6
7
  constructor(config) {
7
8
  super(config, {
8
9
  NodeClient,
9
10
  });
10
- this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode) => async (request, response) => {
11
+ this.handleSignIn = (redirectUri = `${this.config.baseUrl}/api/logto/sign-in-callback`, interactionMode, onError) => buildHandler(async (request, response) => {
11
12
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
12
13
  await nodeClient.signIn(redirectUri, interactionMode);
13
14
  await this.storage?.save();
14
15
  if (this.navigateUrl) {
15
16
  response.redirect(this.navigateUrl);
16
17
  }
17
- };
18
- this.handleSignInCallback = (redirectTo = this.config.baseUrl) => async (request, response) => {
18
+ }, onError);
19
+ this.handleSignInCallback = (redirectTo = this.config.baseUrl, onError) => buildHandler(async (request, response) => {
19
20
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
20
21
  if (request.url) {
21
22
  await nodeClient.handleSignInCallback(`${this.config.baseUrl}${request.url}`);
22
23
  await this.storage?.save();
23
24
  response.redirect(redirectTo);
24
25
  }
25
- };
26
- this.handleSignOut = (redirectUri = this.config.baseUrl) => async (request, response) => {
26
+ }, onError);
27
+ this.handleSignOut = (redirectUri = this.config.baseUrl, onError) => buildHandler(async (request, response) => {
27
28
  const nodeClient = await this.createNodeClientFromNextApi(request, response);
28
29
  await nodeClient.signOut(redirectUri);
29
30
  await this.storage?.destroy();
@@ -31,68 +32,41 @@ class LogtoClient extends LogtoNextBaseClient {
31
32
  if (this.navigateUrl) {
32
33
  response.redirect(this.navigateUrl);
33
34
  }
34
- };
35
- this.handleUser = (configs) => this.withLogtoApiRoute((request, response) => {
35
+ }, onError);
36
+ this.handleUser = (configs, onError) => this.withLogtoApiRoute((request, response) => {
36
37
  response.json(request.user);
37
- }, configs);
38
+ }, configs, onError);
38
39
  this.handleAuthRoutes = (configs, onError) => (request, response) => {
39
- try {
40
- const { action } = request.query;
41
- if (action === 'sign-in') {
42
- return this.handleSignIn()(request, response);
43
- }
44
- if (action === 'sign-up') {
45
- return this.handleSignIn(undefined, 'signUp')(request, response);
46
- }
47
- if (action === 'sign-in-callback') {
48
- return this.handleSignInCallback()(request, response);
49
- }
50
- if (action === 'sign-out') {
51
- return this.handleSignOut()(request, response);
52
- }
53
- if (action === 'user') {
54
- return this.handleUser(configs)(request, response);
55
- }
56
- response.status(404).end();
40
+ const { action } = request.query;
41
+ if (action === 'sign-in') {
42
+ return this.handleSignIn(undefined, undefined, onError)(request, response);
57
43
  }
58
- catch (error) {
59
- if (onError) {
60
- return onError(request, response, error);
61
- }
62
- throw error;
44
+ if (action === 'sign-up') {
45
+ return this.handleSignIn(undefined, 'signUp', onError)(request, response);
63
46
  }
64
- };
65
- this.withLogtoApiRoute = (handler, config = {}, onError) => async (request, response) => {
66
- try {
67
- const nodeClient = await this.createNodeClientFromNextApi(request, response);
68
- const user = await nodeClient.getContext(config);
69
- await this.storage?.save();
70
- // eslint-disable-next-line @silverhand/fp/no-mutating-methods
71
- Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
72
- return handler(request, response);
47
+ if (action === 'sign-in-callback') {
48
+ return this.handleSignInCallback(undefined, onError)(request, response);
49
+ }
50
+ if (action === 'sign-out') {
51
+ return this.handleSignOut(undefined, onError)(request, response);
73
52
  }
74
- catch (error) {
75
- if (onError) {
76
- return onError(request, response, error);
77
- }
78
- throw error;
53
+ if (action === 'user') {
54
+ return this.handleUser(configs)(request, response);
79
55
  }
56
+ response.status(404).end();
80
57
  };
58
+ this.withLogtoApiRoute = (handler, config = {}, onError) => buildHandler(async (request, response) => {
59
+ const nodeClient = await this.createNodeClientFromNextApi(request, response);
60
+ const user = await nodeClient.getContext(config);
61
+ await this.storage?.save();
62
+ // eslint-disable-next-line @silverhand/fp/no-mutating-methods
63
+ Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
64
+ return handler(request, response);
65
+ }, onError);
81
66
  this.withLogtoSsr = (handler, configs = {}, onError) => async (context) => {
82
- try {
83
- const nodeClient = await this.createNodeClientFromNextApi(context.req, context.res);
84
- const user = await nodeClient.getContext(configs);
85
- await this.storage?.save();
86
- // eslint-disable-next-line @silverhand/fp/no-mutating-methods
87
- Object.defineProperty(context.req, 'user', { enumerable: true, get: () => user });
88
- return await handler(context);
89
- }
90
- catch (error) {
91
- if (onError) {
92
- return onError(error);
93
- }
94
- throw error;
95
- }
67
+ return this.withLogtoApiRoute(async () => {
68
+ return handler(context);
69
+ }, configs, onError);
96
70
  };
97
71
  }
98
72
  async createNodeClientFromNextApi(request, response) {
@@ -1,5 +1,6 @@
1
1
  import type { LogtoConfig } from '@logto/node';
2
2
  import type NodeClient from '@logto/node';
3
+ import { type NextApiRequest, type NextApiResponse } from 'next';
3
4
  export type LogtoNextConfig = LogtoConfig & {
4
5
  cookieSecret: string;
5
6
  cookieSecure: boolean;
@@ -8,3 +9,4 @@ export type LogtoNextConfig = LogtoConfig & {
8
9
  export type Adapters = {
9
10
  NodeClient: typeof NodeClient;
10
11
  };
12
+ export type ErrorHandler = (request: NextApiRequest, response: NextApiResponse, error: unknown) => unknown;
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const buildHandler = (handler, onError) => {
4
+ return async (request, response) => {
5
+ try {
6
+ await handler(request, response);
7
+ }
8
+ catch (error) {
9
+ if (onError) {
10
+ return onError(request, response, error);
11
+ }
12
+ throw error;
13
+ }
14
+ };
15
+ };
16
+
17
+ exports.buildHandler = buildHandler;
@@ -0,0 +1,3 @@
1
+ import { type NextApiHandler } from 'next';
2
+ import { type ErrorHandler } from './types';
3
+ export declare const buildHandler: (handler: NextApiHandler, onError?: ErrorHandler) => NextApiHandler;
@@ -0,0 +1,15 @@
1
+ const buildHandler = (handler, onError) => {
2
+ return async (request, response) => {
3
+ try {
4
+ await handler(request, response);
5
+ }
6
+ catch (error) {
7
+ if (onError) {
8
+ return onError(request, response, error);
9
+ }
10
+ throw error;
11
+ }
12
+ };
13
+ };
14
+
15
+ export { buildHandler };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/next",
3
- "version": "3.3.4",
3
+ "version": "3.5.0",
4
4
  "type": "module",
5
5
  "main": "./lib/src/index.cjs",
6
6
  "module": "./lib/src/index.js",