kenny-login 0.0.2 → 0.0.3

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.
package/dist/auth.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { type NextAuthResult } from "next-auth";
2
2
  export declare const auth: NextAuthResult["auth"];
3
3
  export declare const handlers: {
4
- POST: (req: import("next/server").NextRequest) => Promise<Response>;
5
4
  GET: (req: import("next/server").NextRequest) => Promise<Response>;
5
+ POST: (req: import("next/server").NextRequest) => Promise<Response>;
6
6
  };
7
7
  export declare const signIn: <P extends import("@auth/core/providers").ProviderId, R extends boolean = true>(provider?: P, options?: FormData | ({
8
8
  redirectTo?: string;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- export { handlers, signIn, signOut, auth } from "./auth";
1
+ export { handlers as strapiHandlers, signIn as strapiSignIn, signOut as strapiSignOut, auth as strapiAuth } from "./strapiAuth";
2
2
  export { SignInForm } from "./components/signin-form";
3
+ export { ApiClient } from "./utils/ApiClient";
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,IAAI,YAAY,EAAE,OAAO,IAAI,aAAa,EAAE,IAAI,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AAEhI,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
- export { handlers, signIn, signOut, auth } from "./auth";
1
+ // NextAuth exports are exported as "strapi{export}" so that we can easily support additional API services
2
+ export { handlers as strapiHandlers, signIn as strapiSignIn, signOut as strapiSignOut, auth as strapiAuth } from "./strapiAuth";
2
3
  export { SignInForm } from "./components/signin-form";
4
+ export { ApiClient } from "./utils/ApiClient";
3
5
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0GAA0G;AAC1G,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,IAAI,YAAY,EAAE,OAAO,IAAI,aAAa,EAAE,IAAI,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AAEhI,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { type NextAuthResult } from "next-auth";
2
+ export declare const auth: NextAuthResult["auth"];
3
+ export declare const handlers: {
4
+ GET: (req: import("next/server").NextRequest) => Promise<Response>;
5
+ POST: (req: import("next/server").NextRequest) => Promise<Response>;
6
+ };
7
+ export declare const signIn: <P extends import("@auth/core/providers").ProviderId, R extends boolean = true>(provider?: P, options?: FormData | ({
8
+ redirectTo?: string;
9
+ redirect?: R;
10
+ } & Record<string, any>), authorizationParams?: string[][] | Record<string, string> | string | URLSearchParams) => Promise<R extends false ? any : never>;
11
+ export declare const signOut: <R extends boolean = true>(options?: {
12
+ redirectTo?: string;
13
+ redirect?: R;
14
+ }) => Promise<R extends false ? any : never>;
15
+ //# sourceMappingURL=strapiAuth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strapiAuth.d.ts","sourceRoot":"","sources":["../src/strapiAuth.ts"],"names":[],"mappings":"AAAA,OAAiB,EAAE,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAgH1D,eAAO,MAAM,IAAI,EAAG,cAAc,CAAC,MAAM,CAAuB,CAAC;AACjE,eAAO,MAAM,QAAQ;;;CAA0B,CAAC;AAChD,eAAO,MAAM,MAAM;cAEmuM,CAAC;YAAyJ,CAAC;yJAFt2M,CAAC;AAC5C,eAAO,MAAM,OAAO;cACk/O,CAAC;YAA0J,CAAC;4CADrnP,CAAC"}
@@ -0,0 +1,100 @@
1
+ import NextAuth, {} from "next-auth";
2
+ import { SignInError } from "@auth/core/errors";
3
+ import Google from "next-auth/providers/google";
4
+ import Credentials from "next-auth/providers/credentials";
5
+ import MicrosoftEntraID from "next-auth/providers/microsoft-entra-id";
6
+ import { ApiClient } from "./utils/ApiClient";
7
+ async function refreshToken(token) {
8
+ try {
9
+ const url = new URL(`${process.env.NEXT_PUBLIC_API_URL}/api/auth/refresh`);
10
+ const response = await fetch(url, {
11
+ headers: ApiClient.defaultHeaders,
12
+ method: 'POST',
13
+ body: JSON.stringify({ refreshToken: token })
14
+ });
15
+ const refreshedToken = await response.json();
16
+ if (!response.ok) {
17
+ throw refreshedToken;
18
+ }
19
+ token.jwt = refreshedToken.jwt;
20
+ return token;
21
+ }
22
+ catch (error) {
23
+ console.log(error);
24
+ return {
25
+ ...token,
26
+ error: "RefreshAccessTokenError",
27
+ };
28
+ }
29
+ }
30
+ const nextAuthResult = NextAuth({
31
+ providers: [
32
+ Google,
33
+ MicrosoftEntraID,
34
+ Credentials({
35
+ credentials: {
36
+ email: {
37
+ type: "email",
38
+ label: "Email"
39
+ },
40
+ password: {
41
+ type: "password",
42
+ label: "Password"
43
+ },
44
+ },
45
+ authorize: async (credentials) => {
46
+ const response = await fetch(new URL(`${process.env.NEXT_PUBLIC_API_URL}/auth/local`), {
47
+ method: 'POST',
48
+ headers: ApiClient.defaultHeaders,
49
+ body: JSON.stringify({
50
+ identifier: credentials.email,
51
+ password: credentials.password
52
+ })
53
+ });
54
+ const data = await response.json();
55
+ if (response.status != 200) {
56
+ throw new SignInError(data.error.message);
57
+ }
58
+ const user = { name: data.user.username, email: data.user.email, jwt: data.jwt };
59
+ return user;
60
+ }
61
+ })
62
+ ],
63
+ session: {
64
+ strategy: "jwt"
65
+ },
66
+ callbacks: {
67
+ async session({ session, token }) {
68
+ if (token) {
69
+ session.jwt = token.jwt;
70
+ }
71
+ return session;
72
+ },
73
+ async jwt({ token, user, account }) {
74
+ if (account && user) {
75
+ try {
76
+ const strapiProviderName = account.provider === "microsoft-entra-id"
77
+ ? "microsoft" : account.provider;
78
+ const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/${strapiProviderName}/callback?access_token=${account.access_token}`);
79
+ const data = await response.json();
80
+ token.jwt = data.jwt;
81
+ return token;
82
+ }
83
+ catch (error) {
84
+ console.error("Error in JWT callback:", error);
85
+ }
86
+ }
87
+ // Return previous token if the access token has not expired yet
88
+ if (token.exp && Date.now() < token.exp) {
89
+ return token;
90
+ }
91
+ // Access token has expired, try to update it
92
+ return refreshToken(token);
93
+ }
94
+ }
95
+ });
96
+ export const auth = nextAuthResult.auth;
97
+ export const handlers = nextAuthResult.handlers;
98
+ export const signIn = nextAuthResult.signIn;
99
+ export const signOut = nextAuthResult.signOut;
100
+ //# sourceMappingURL=strapiAuth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strapiAuth.js","sourceRoot":"","sources":["../src/strapiAuth.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,EAAE,EAAuB,MAAM,WAAW,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,MAAM,MAAM,4BAA4B,CAAA;AAC/C,OAAO,WAAW,MAAM,iCAAiC,CAAC;AAC1D,OA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AQ,GAAG,MAAM,KAAK,CAC1B,GAAG,OAAO,CAAC,GAAG,CAAC,mBAAmB,SAAS,kBAAkB,0BAA0B,OAAO,CAAC,YAAY,EAAE,CAC9G,CAAC;oBACF,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;oBACnC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC;oBACrB,OAAO,KAAK,CAAC;gBACf,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,KAAK,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;gBACjD,CAAC;YACH,CAAC;YAED,gEAAgE;YAChE,IAAI,KAAK,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC;gBACxC,OAAO,KAAK,CAAC;YACf,CAAC;YAED,6CAA6C;YAC7C,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC;QAC7B,CAAC;KACF;CACF,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,IAAI,GAA4B,cAAc,CAAC,IAAI,CAAC;AACjE,MAAM,CAAC,MAAM,QAAQ,GAAG,cAAc,CAAC,QAAQ,CAAC;AAChD,MAAM,CAAC,MAAM,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC;AAC5C,MAAM,CAAC,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC"}
@@ -0,0 +1,80 @@
1
+ export declare class ApiClient {
2
+ private apiBaseUrl;
3
+ static defaultHeaders: {
4
+ 'Content-Type': string;
5
+ };
6
+ static genericOptions: {
7
+ headers: {
8
+ 'Content-Type': string;
9
+ };
10
+ };
11
+ /**
12
+ * @param {string} apiBaseUrl - The base url of your API service. This should include any path prefixes (like "/api/", "/api/v1/", etc.)
13
+ * @param {string|null} refreshEndpoint — (Optional) The path of your jwt refresh endpoint, if it exists.
14
+ */
15
+ constructor(apiBaseUrl: string);
16
+ private getToken;
17
+ private makeRequest;
18
+ /**
19
+ * Make an arbitrary request to an endpoint that does not require authentication.
20
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
21
+ * @param {RequestInit} options — Request options to be passed into fetch()
22
+ */
23
+ makePublicRequest(endpoint: string, options: RequestInit): Promise<any>;
24
+ /**
25
+ * Make an arbitrary request using the logged-in user's session.
26
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
27
+ * @param {RequestInit} options — Request options to be passed into fetch()
28
+ */
29
+ makePrivateRequest(endpoint: string, options: RequestInit): Promise<any>;
30
+ /**
31
+ * Make a GET request to an endpoint that does not require authentication.
32
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
33
+ */
34
+ getPublic(endpoint: string): Promise<Object>;
35
+ /**
36
+ * Make a GET request using the logged-in user's session.
37
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
38
+ */
39
+ getPrivate(endpoint: string): Promise<Object>;
40
+ /**
41
+ * Make a POST request to an endpoint that does not require authentication.
42
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
43
+ * @param {Object} postData — The data to send with your POST request
44
+ */
45
+ postPublic(endpoint: string, postData: Object): Promise<Object>;
46
+ /**
47
+ * Make a POST request using the logged-in user's session.
48
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
49
+ * @param {Object} postData — The data to send with your POST request
50
+ */
51
+ postPrivate(endpoint: string, postData: Object): Promise<Object>;
52
+ /**
53
+ * Make a PUT request to an endpoint that does not require authentication.
54
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
55
+ * @param {Object} putData — The data to send with your PUT request
56
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be updated. Will be appended to the provided endpoint path.
57
+ */
58
+ putPublic(endpoint: string, putData: Object, id?: string | number | null): Promise<Object>;
59
+ /**
60
+ * Make a PUT request using the logged-in user's session.
61
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
62
+ * @param {Object} putData — The data to send with your PUT request
63
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be updated. Will be appended to the provided endpoint path.
64
+ */
65
+ putPrivate(endpoint: string, putData: Object, id?: string | number | null): Promise<Object>;
66
+ /**
67
+ * Make a DELETE request to an endpoint that does not require authentication.
68
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
69
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be deleted. Will be appended to the provided endpoint path.
70
+ */
71
+ deletePublic(endpoint: string, id?: string | number | null): Promise<any>;
72
+ /**
73
+ * Make a DELETE request using the logged-in user's session.
74
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
75
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be deleted. Will be appended to the provided endpoint path.
76
+ */
77
+ deletePrivate(endpoint: string, id?: string | number | null): Promise<any>;
78
+ private appendIdToEndpoint;
79
+ }
80
+ //# sourceMappingURL=ApiClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiClient.d.ts","sourceRoot":"","sources":["../../src/utils/ApiClient.ts"],"names":[],"mappings":"AAGA,qBAAa,SAAS;IAed,OAAO,CAAC,UAAU;IAbtB,OAAc,cAAc;;MAE3B;IAED,OAAc,cAAc;;;;MAE3B;IAED;;;MAGE;gBAEU,UAAU,EAAG,MAAM;YAGjB,QAAQ;YAWR,WAAW;IAOzB;;;;MAIE;IACW,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW;IAIrE;;;;MAIE;IACW,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW;IAStE;;;MAGE;IACW,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAI,OAAO,CAAC,MAAM,CAAC;IAQ1D;;;MAGE;IACW,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAI,OAAO,CAAC,MAAM,CAAC;IAQ3D;;;;MAIE;IACW,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAI,OAAO,CAAC,MAAM,CAAC;IAS7E;;;;MAIE;IACW,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAI,OAAO,CAAC,MAAM,CAAC;IAS9E;;;;;MAKE;IACW,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,GAAG,MAAM,GAAG,MAAM,GAAG,IAAW,GAAI,OAAO,CAAC,MAAM,CAAC;IAU/G;;;;;MAKE;IACW,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,GAAG,MAAM,GAAG,MAAM,GAAG,IAAW,GAAI,OAAO,CAAC,MAAM,CAAC;IAUhH;;;;MAIE;IACW,YAAY,CAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,GAAE,MAAM,GAAG,MAAM,GAAG,IAAW;IAS9E;;;;MAIE;IACW,aAAa,CAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,GAAE,MAAM,GAAG,MAAM,GAAG,IAAW;IAS/E,OAAO,CAAC,kBAAkB;CAe7B"}
@@ -0,0 +1,170 @@
1
+ import { AuthError } from 'next-auth';
2
+ import { getSession } from 'next-auth/react';
3
+ export class ApiClient {
4
+ apiBaseUrl;
5
+ static defaultHeaders = {
6
+ 'Content-Type': 'application/json'
7
+ };
8
+ static genericOptions = {
9
+ headers: ApiClient.defaultHeaders
10
+ };
11
+ /**
12
+ * @param {string} apiBaseUrl - The base url of your API service. This should include any path prefixes (like "/api/", "/api/v1/", etc.)
13
+ * @param {string|null} refreshEndpoint — (Optional) The path of your jwt refresh endpoint, if it exists.
14
+ */
15
+ constructor(apiBaseUrl) {
16
+ this.apiBaseUrl = apiBaseUrl;
17
+ }
18
+ async getToken() {
19
+ const session = await getSession();
20
+ if (!session) {
21
+ throw new AuthError('Tried to get session token with no active session!');
22
+ }
23
+ const token = session?.jwt;
24
+ return token;
25
+ }
26
+ async makeRequest(endpoint, options) {
27
+ const url = new URL(endpoint, this.apiBaseUrl);
28
+ const response = await fetch(url, options);
29
+ const data = await response.json();
30
+ return data;
31
+ }
32
+ /**
33
+ * Make an arbitrary request to an endpoint that does not require authentication.
34
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
35
+ * @param {RequestInit} options — Request options to be passed into fetch()
36
+ */
37
+ async makePublicRequest(endpoint, options) {
38
+ return this.makeRequest(endpoint, options);
39
+ }
40
+ /**
41
+ * Make an arbitrary request using the logged-in user's session.
42
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
43
+ * @param {RequestInit} options — Request options to be passed into fetch()
44
+ */
45
+ async makePrivateRequest(endpoint, options) {
46
+ const token = this.getToken();
47
+ options.headers = {
48
+ ...options.headers,
49
+ Authorization: `Bearer ${token}`
50
+ };
51
+ return await this.makeRequest(endpoint, options);
52
+ }
53
+ /**
54
+ * Make a GET request to an endpoint that does not require authentication.
55
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
56
+ */
57
+ async getPublic(endpoint) {
58
+ const options = {
59
+ ...ApiClient.genericOptions,
60
+ method: 'GET'
61
+ };
62
+ return await this.makeRequest(endpoint, options);
63
+ }
64
+ /**
65
+ * Make a GET request using the logged-in user's session.
66
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
67
+ */
68
+ async getPrivate(endpoint) {
69
+ const options = {
70
+ ...ApiClient.genericOptions,
71
+ method: 'GET'
72
+ };
73
+ return await this.makePrivateRequest(endpoint, options);
74
+ }
75
+ /**
76
+ * Make a POST request to an endpoint that does not require authentication.
77
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
78
+ * @param {Object} postData — The data to send with your POST request
79
+ */
80
+ async postPublic(endpoint, postData) {
81
+ const options = {
82
+ ...ApiClient.genericOptions,
83
+ method: 'POST',
84
+ body: JSON.stringify({ data: postData })
85
+ };
86
+ return await this.makeRequest(endpoint, options);
87
+ }
88
+ /**
89
+ * Make a POST request using the logged-in user's session.
90
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
91
+ * @param {Object} postData — The data to send with your POST request
92
+ */
93
+ async postPrivate(endpoint, postData) {
94
+ const options = {
95
+ ...ApiClient.genericOptions,
96
+ method: 'POST',
97
+ body: JSON.stringify({ data: postData })
98
+ };
99
+ return await this.makePrivateRequest(endpoint, options);
100
+ }
101
+ /**
102
+ * Make a PUT request to an endpoint that does not require authentication.
103
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
104
+ * @param {Object} putData — The data to send with your PUT request
105
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be updated. Will be appended to the provided endpoint path.
106
+ */
107
+ async putPublic(endpoint, putData, id = null) {
108
+ endpoint = this.appendIdToEndpoint(endpoint, id);
109
+ const options = {
110
+ ...ApiClient.genericOptions,
111
+ method: 'PUT',
112
+ body: JSON.stringify({ data: putData })
113
+ };
114
+ return await this.makeRequest(endpoint, options);
115
+ }
116
+ /**
117
+ * Make a PUT request using the logged-in user's session.
118
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
119
+ * @param {Object} putData — The data to send with your PUT request
120
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be updated. Will be appended to the provided endpoint path.
121
+ */
122
+ async putPrivate(endpoint, putData, id = null) {
123
+ endpoint = this.appendIdToEndpoint(endpoint, id);
124
+ const options = {
125
+ ...ApiClient.genericOptions,
126
+ method: 'PUT',
127
+ body: JSON.stringify({ data: putData })
128
+ };
129
+ return await this.makePrivateRequest(endpoint, options);
130
+ }
131
+ /**
132
+ * Make a DELETE request to an endpoint that does not require authentication.
133
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
134
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be deleted. Will be appended to the provided endpoint path.
135
+ */
136
+ async deletePublic(endpoint, id = null) {
137
+ endpoint = this.appendIdToEndpoint(endpoint, id);
138
+ const options = {
139
+ ...ApiClient.genericOptions,
140
+ method: 'DELETE'
141
+ };
142
+ return await this.makeRequest(endpoint, options);
143
+ }
144
+ /**
145
+ * Make a DELETE request using the logged-in user's session.
146
+ * @param {string} endpoint - The endpoint to send the request to. Should exclude your API's base url.
147
+ * @param {string|number|null} id — (Optional) The identifier for the resource to be deleted. Will be appended to the provided endpoint path.
148
+ */
149
+ async deletePrivate(endpoint, id = null) {
150
+ endpoint = this.appendIdToEndpoint(endpoint, id);
151
+ const options = {
152
+ ...ApiClient.genericOptions,
153
+ method: 'DELETE'
154
+ };
155
+ return await this.makePrivateRequest(endpoint, options);
156
+ }
157
+ appendIdToEndpoint(endpoint, id = null) {
158
+ if (typeof id === 'number') {
159
+ id = id.toString();
160
+ }
161
+ if (endpoint.slice(-1) !== '/') {
162
+ endpoint = endpoint + '/';
163
+ }
164
+ if (id !== null) {
165
+ endpoint = `${endpoint}${id}`;
166
+ }
167
+ return endpoint;
168
+ }
169
+ }
170
+ //# sourceMappingURL=ApiClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiClient.js","sourceRoot":"","sources":["../../src/utils/ApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,MAAM,OAAO,SAAS;IAeN;IAbL,MAAM,CAAC,cAAc,GAAG;QAC3B,cAAc,EAAG,kBAAkB;KACtC,CAAA;IAEM,MAAM,CAAC,cAAc,GAAG;QAC3B,OAAO,EAAE,SAAS,CAAC,cAAc;KACpC,CAAA;IAED;;;MAGE;IACF,YACY,UAAmB;QAAnB,eAAU,GAAV,UAAU,CAAS;IAC3B,CAAC;IAEG,KAAK,CAAC,QAAQ;QAClB,MAAM,OAAO,GAAG,MAAM,UAAU,EAAE,CAAC;QAEnC,IAAI,CAAC,OAAO,EAAE,CAAC;YACX,MAAM,IAAI,SAAS,CAAC,oDAAoD,CAAC,CAAC;QAC9E,CAAC;QAED,MAAM,KAAK,GA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