@heymantle/core-api-client 0.3.5 → 0.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.
- package/dist/index.d.mts +5 -45
- package/dist/index.d.ts +5 -45
- package/dist/index.js +8 -46
- package/dist/index.mjs +8 -45
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -27544,7 +27544,7 @@ declare class MantleCoreClient {
|
|
|
27544
27544
|
*/
|
|
27545
27545
|
eject(middleware: Middleware): this;
|
|
27546
27546
|
/**
|
|
27547
|
-
* Update authentication credentials
|
|
27547
|
+
* Update authentication credentials at runtime.
|
|
27548
27548
|
*/
|
|
27549
27549
|
updateAuth(credentials: {
|
|
27550
27550
|
apiKey?: string;
|
|
@@ -27731,7 +27731,9 @@ declare class MantleAPIError extends Error {
|
|
|
27731
27731
|
* Authentication failed (401)
|
|
27732
27732
|
*/
|
|
27733
27733
|
declare class MantleAuthenticationError extends MantleAPIError {
|
|
27734
|
-
|
|
27734
|
+
/** Whether the token has expired (as opposed to being invalid/malformed) */
|
|
27735
|
+
readonly isExpired: boolean;
|
|
27736
|
+
constructor(message?: string, isExpired?: boolean);
|
|
27735
27737
|
}
|
|
27736
27738
|
/**
|
|
27737
27739
|
* Permission denied (403)
|
|
@@ -27759,48 +27761,6 @@ declare class MantleRateLimitError extends MantleAPIError {
|
|
|
27759
27761
|
constructor(message?: string, retryAfter?: number);
|
|
27760
27762
|
}
|
|
27761
27763
|
|
|
27762
|
-
/**
|
|
27763
|
-
* Options for the auth refresh middleware
|
|
27764
|
-
*/
|
|
27765
|
-
interface AuthRefreshOptions {
|
|
27766
|
-
/**
|
|
27767
|
-
* Function to refresh the access token.
|
|
27768
|
-
* Should return the new access token.
|
|
27769
|
-
*/
|
|
27770
|
-
refreshToken: () => Promise<string>;
|
|
27771
|
-
/**
|
|
27772
|
-
* Callback invoked when the client instance's auth should be updated.
|
|
27773
|
-
* Receives the new token string.
|
|
27774
|
-
*/
|
|
27775
|
-
updateAuth: (newToken: string) => void;
|
|
27776
|
-
/** Optional callback when refresh succeeds */
|
|
27777
|
-
onRefreshSuccess?: (newToken: string) => void;
|
|
27778
|
-
/** Optional callback when refresh fails */
|
|
27779
|
-
onRefreshFailed?: (error: Error) => void;
|
|
27780
|
-
/** Maximum refresh attempts per request (default: 1) */
|
|
27781
|
-
maxRefreshAttempts?: number;
|
|
27782
|
-
}
|
|
27783
|
-
/**
|
|
27784
|
-
* Creates an openapi-fetch middleware that automatically refreshes access
|
|
27785
|
-
* tokens on 401 responses.
|
|
27786
|
-
*
|
|
27787
|
-
* @example
|
|
27788
|
-
* ```typescript
|
|
27789
|
-
* const client = new MantleCoreClient({ accessToken: 'initial-token' });
|
|
27790
|
-
*
|
|
27791
|
-
* client.use(createAuthRefreshMiddleware({
|
|
27792
|
-
* refreshToken: async () => {
|
|
27793
|
-
* const res = await fetch('/refresh', { method: 'POST' });
|
|
27794
|
-
* const data = await res.json();
|
|
27795
|
-
* return data.accessToken;
|
|
27796
|
-
* },
|
|
27797
|
-
* updateAuth: (newToken) => client.updateAuth({ accessToken: newToken }),
|
|
27798
|
-
* onRefreshSuccess: (newToken) => localStorage.setItem('accessToken', newToken),
|
|
27799
|
-
* }));
|
|
27800
|
-
* ```
|
|
27801
|
-
*/
|
|
27802
|
-
declare function createAuthRefreshMiddleware(options: AuthRefreshOptions): Middleware;
|
|
27803
|
-
|
|
27804
27764
|
/**
|
|
27805
27765
|
* Options for the rate limit middleware
|
|
27806
27766
|
*/
|
|
@@ -27839,4 +27799,4 @@ interface RateLimitOptions {
|
|
|
27839
27799
|
*/
|
|
27840
27800
|
declare function createRateLimitMiddleware(options?: RateLimitOptions): Middleware;
|
|
27841
27801
|
|
|
27842
|
-
export { type AccountOwner, type AccountOwnerType, type Action, type ActiveSubscriptionsResponse, type Affiliate, type AffiliateAttributionWebhook, type AffiliateCommission, AffiliateCommissionsResource, type AffiliateMembershipWebhook, type AffiliatePayout, type AffiliatePayoutWebhook, AffiliatePayoutsResource, type AffiliateProgram, type AffiliateProgramGroup, type AffiliateProgramMembership, type AffiliateProgramWebhook, AffiliateProgramsResource, type AffiliateReferral, AffiliateReferralsResource, type AffiliateRules, type AffiliateRulesWebhook, type AffiliateWebhook, AffiliatesResource, type Agent, type AgentRun, AgentsResource, AiAgentRunsResource, type App, type AppChargeWebhook, type AppEvent, type AppInstallation, type AppInstallationWebhook, type AppWebhook, AppsResource, type
|
|
27802
|
+
export { type AccountOwner, type AccountOwnerType, type Action, type ActiveSubscriptionsResponse, type Affiliate, type AffiliateAttributionWebhook, type AffiliateCommission, AffiliateCommissionsResource, type AffiliateMembershipWebhook, type AffiliatePayout, type AffiliatePayoutWebhook, AffiliatePayoutsResource, type AffiliateProgram, type AffiliateProgramGroup, type AffiliateProgramMembership, type AffiliateProgramWebhook, AffiliateProgramsResource, type AffiliateReferral, AffiliateReferralsResource, type AffiliateRules, type AffiliateRulesWebhook, type AffiliateWebhook, AffiliatesResource, type Agent, type AgentRun, AgentsResource, AiAgentRunsResource, type App, type AppChargeWebhook, type AppEvent, type AppInstallation, type AppInstallationWebhook, type AppWebhook, AppsResource, type BaseCustomerWebhook, BaseResource, ChannelsResource, type Charge, ChargesResource, type Checklist, type ChecklistStep, CompaniesResource, type Company, type Contact, type ContactWebhook, ContactsResource, type CursorPagination, CustomDataResource, type CustomField, type CustomFieldDefaultValue, type Customer, type CustomerContact, type CustomerContactWebhook, type CustomerSegment, CustomerSegmentsResource, type CustomerWebhook, type CustomerWithoutSubscriptionWebhook, CustomersResource, type Deal, type DealAcquirer, DealActivitiesResource, type DealActivity, type DealAffiliate, type DealApp, type DealContact, type DealCustomData, type DealCustomer, type DealEvent, type DealFlow, type DealFlowAffiliate, type DealFlowDefaultSource, type DealFlowPartnership, type DealFlowStage, type DealFlowUser, DealFlowsResource, type DealOwner, type DealPartnership, type DealPlan, type DealStage, DealsResource, type DetailedAffiliate, type DetailedFlow, type DocsCollection, type DocsCollectionResponse, type DocsGenerateJobResponse, type DocsGenerateStatusCompleted, type DocsGenerateStatusFailed, type DocsGenerateStatusPending, type DocsGroup, type DocsGroupResponse, type DocsPage, type DocsPageResponse, type DocsPageSummary, type DocsRepository, DocsResource, type EmailCampaign, type EmailDelivery, type EmailDeliveryStats, type EmailLayout, type EmailPreview, type EmailSender, type EmailUnsubscribeGroup, type EmailUnsubscribeGroupMember, EmailUnsubscribeGroupsResource, EntitiesResource, type EventWebhook, type ExtensionUninstallPayload, type ExtensionWebhook, type Feature, type Flow, type FlowActionRun, FlowExtensionsResource, FlowsResource, type InvalidTokenError, type Invoice, type InvoiceWebhook, JournalEntriesResource, type JournalEntry, type JournalEntryApp, type JournalEntryFile, type List, ListsResource, type LogoChurnResponse, MantleAPIError, MantleAuthenticationError, MantleCoreClient, type MantleCoreClientConfig, MantleNotFoundError, MantlePermissionError, MantleRateLimitError, MantleValidationError, MeResource, type Meeting, type MeetingAttendee, type MeetingAttendeeRef, type MeetingContactRef, type MeetingCustomer, type MeetingDeal, type MeetingPermission, type MeetingTaskSuggestion, type MeetingTranscript, type MeetingUser, type MeetingUtterance, MeetingsResource, MetricsResource, type MissingScopesError, type MrrResponse, type NetInstallsResponse, type NetRevenueResponse, type NetRevenueRetentionResponse, type Organization, OrganizationResource, type Pagination, type PayoutResponse, type Plan, type PlanFeature, type PlanFeatureWebhook, type PlanUsageChargeWebhook, type PlanWebhook, type PredictedLtvResponse, type RateLimitOptions, type RevenueRetentionResponse, type Review, type ReviewWebhook, type StandardMetricResponse, type Subscription, type SubscriptionWebhook, SubscriptionsResource, type SyncedEmail, type SyncedEmailContact, type SyncedEmailCustomer, type SyncedEmailDeal, type SyncedEmailMessage, type SyncedEmailUser, SyncedEmailsResource, type Task, type TaskComment, type TaskTodoItem, TasksResource, type ThreadEventType, type ThreadMessageWebhook, type ThreadWebhook, type Ticket, type TicketAgent, type TicketApp, type TicketAttachment, type TicketChannel, type TicketContact, type TicketCustomer, type TicketEvent, type TicketLoop, type TicketMessage, TicketsResource, type TimeSeriesDataPoint, type TimelineComment, TimelineCommentsResource, type Transaction, type TransactionWebhook, TransactionsResource, type Trigger, type UninstallEventWebhook, type UsageEvent, type UsageEventResponse, UsageEventsResource, type UsageMetric, type UsageMetricResponse, type User, UsersResource, type Webhook, WebhooksResource, type components, createRateLimitMiddleware, type paths };
|
package/dist/index.d.ts
CHANGED
|
@@ -27544,7 +27544,7 @@ declare class MantleCoreClient {
|
|
|
27544
27544
|
*/
|
|
27545
27545
|
eject(middleware: Middleware): this;
|
|
27546
27546
|
/**
|
|
27547
|
-
* Update authentication credentials
|
|
27547
|
+
* Update authentication credentials at runtime.
|
|
27548
27548
|
*/
|
|
27549
27549
|
updateAuth(credentials: {
|
|
27550
27550
|
apiKey?: string;
|
|
@@ -27731,7 +27731,9 @@ declare class MantleAPIError extends Error {
|
|
|
27731
27731
|
* Authentication failed (401)
|
|
27732
27732
|
*/
|
|
27733
27733
|
declare class MantleAuthenticationError extends MantleAPIError {
|
|
27734
|
-
|
|
27734
|
+
/** Whether the token has expired (as opposed to being invalid/malformed) */
|
|
27735
|
+
readonly isExpired: boolean;
|
|
27736
|
+
constructor(message?: string, isExpired?: boolean);
|
|
27735
27737
|
}
|
|
27736
27738
|
/**
|
|
27737
27739
|
* Permission denied (403)
|
|
@@ -27759,48 +27761,6 @@ declare class MantleRateLimitError extends MantleAPIError {
|
|
|
27759
27761
|
constructor(message?: string, retryAfter?: number);
|
|
27760
27762
|
}
|
|
27761
27763
|
|
|
27762
|
-
/**
|
|
27763
|
-
* Options for the auth refresh middleware
|
|
27764
|
-
*/
|
|
27765
|
-
interface AuthRefreshOptions {
|
|
27766
|
-
/**
|
|
27767
|
-
* Function to refresh the access token.
|
|
27768
|
-
* Should return the new access token.
|
|
27769
|
-
*/
|
|
27770
|
-
refreshToken: () => Promise<string>;
|
|
27771
|
-
/**
|
|
27772
|
-
* Callback invoked when the client instance's auth should be updated.
|
|
27773
|
-
* Receives the new token string.
|
|
27774
|
-
*/
|
|
27775
|
-
updateAuth: (newToken: string) => void;
|
|
27776
|
-
/** Optional callback when refresh succeeds */
|
|
27777
|
-
onRefreshSuccess?: (newToken: string) => void;
|
|
27778
|
-
/** Optional callback when refresh fails */
|
|
27779
|
-
onRefreshFailed?: (error: Error) => void;
|
|
27780
|
-
/** Maximum refresh attempts per request (default: 1) */
|
|
27781
|
-
maxRefreshAttempts?: number;
|
|
27782
|
-
}
|
|
27783
|
-
/**
|
|
27784
|
-
* Creates an openapi-fetch middleware that automatically refreshes access
|
|
27785
|
-
* tokens on 401 responses.
|
|
27786
|
-
*
|
|
27787
|
-
* @example
|
|
27788
|
-
* ```typescript
|
|
27789
|
-
* const client = new MantleCoreClient({ accessToken: 'initial-token' });
|
|
27790
|
-
*
|
|
27791
|
-
* client.use(createAuthRefreshMiddleware({
|
|
27792
|
-
* refreshToken: async () => {
|
|
27793
|
-
* const res = await fetch('/refresh', { method: 'POST' });
|
|
27794
|
-
* const data = await res.json();
|
|
27795
|
-
* return data.accessToken;
|
|
27796
|
-
* },
|
|
27797
|
-
* updateAuth: (newToken) => client.updateAuth({ accessToken: newToken }),
|
|
27798
|
-
* onRefreshSuccess: (newToken) => localStorage.setItem('accessToken', newToken),
|
|
27799
|
-
* }));
|
|
27800
|
-
* ```
|
|
27801
|
-
*/
|
|
27802
|
-
declare function createAuthRefreshMiddleware(options: AuthRefreshOptions): Middleware;
|
|
27803
|
-
|
|
27804
27764
|
/**
|
|
27805
27765
|
* Options for the rate limit middleware
|
|
27806
27766
|
*/
|
|
@@ -27839,4 +27799,4 @@ interface RateLimitOptions {
|
|
|
27839
27799
|
*/
|
|
27840
27800
|
declare function createRateLimitMiddleware(options?: RateLimitOptions): Middleware;
|
|
27841
27801
|
|
|
27842
|
-
export { type AccountOwner, type AccountOwnerType, type Action, type ActiveSubscriptionsResponse, type Affiliate, type AffiliateAttributionWebhook, type AffiliateCommission, AffiliateCommissionsResource, type AffiliateMembershipWebhook, type AffiliatePayout, type AffiliatePayoutWebhook, AffiliatePayoutsResource, type AffiliateProgram, type AffiliateProgramGroup, type AffiliateProgramMembership, type AffiliateProgramWebhook, AffiliateProgramsResource, type AffiliateReferral, AffiliateReferralsResource, type AffiliateRules, type AffiliateRulesWebhook, type AffiliateWebhook, AffiliatesResource, type Agent, type AgentRun, AgentsResource, AiAgentRunsResource, type App, type AppChargeWebhook, type AppEvent, type AppInstallation, type AppInstallationWebhook, type AppWebhook, AppsResource, type
|
|
27802
|
+
export { type AccountOwner, type AccountOwnerType, type Action, type ActiveSubscriptionsResponse, type Affiliate, type AffiliateAttributionWebhook, type AffiliateCommission, AffiliateCommissionsResource, type AffiliateMembershipWebhook, type AffiliatePayout, type AffiliatePayoutWebhook, AffiliatePayoutsResource, type AffiliateProgram, type AffiliateProgramGroup, type AffiliateProgramMembership, type AffiliateProgramWebhook, AffiliateProgramsResource, type AffiliateReferral, AffiliateReferralsResource, type AffiliateRules, type AffiliateRulesWebhook, type AffiliateWebhook, AffiliatesResource, type Agent, type AgentRun, AgentsResource, AiAgentRunsResource, type App, type AppChargeWebhook, type AppEvent, type AppInstallation, type AppInstallationWebhook, type AppWebhook, AppsResource, type BaseCustomerWebhook, BaseResource, ChannelsResource, type Charge, ChargesResource, type Checklist, type ChecklistStep, CompaniesResource, type Company, type Contact, type ContactWebhook, ContactsResource, type CursorPagination, CustomDataResource, type CustomField, type CustomFieldDefaultValue, type Customer, type CustomerContact, type CustomerContactWebhook, type CustomerSegment, CustomerSegmentsResource, type CustomerWebhook, type CustomerWithoutSubscriptionWebhook, CustomersResource, type Deal, type DealAcquirer, DealActivitiesResource, type DealActivity, type DealAffiliate, type DealApp, type DealContact, type DealCustomData, type DealCustomer, type DealEvent, type DealFlow, type DealFlowAffiliate, type DealFlowDefaultSource, type DealFlowPartnership, type DealFlowStage, type DealFlowUser, DealFlowsResource, type DealOwner, type DealPartnership, type DealPlan, type DealStage, DealsResource, type DetailedAffiliate, type DetailedFlow, type DocsCollection, type DocsCollectionResponse, type DocsGenerateJobResponse, type DocsGenerateStatusCompleted, type DocsGenerateStatusFailed, type DocsGenerateStatusPending, type DocsGroup, type DocsGroupResponse, type DocsPage, type DocsPageResponse, type DocsPageSummary, type DocsRepository, DocsResource, type EmailCampaign, type EmailDelivery, type EmailDeliveryStats, type EmailLayout, type EmailPreview, type EmailSender, type EmailUnsubscribeGroup, type EmailUnsubscribeGroupMember, EmailUnsubscribeGroupsResource, EntitiesResource, type EventWebhook, type ExtensionUninstallPayload, type ExtensionWebhook, type Feature, type Flow, type FlowActionRun, FlowExtensionsResource, FlowsResource, type InvalidTokenError, type Invoice, type InvoiceWebhook, JournalEntriesResource, type JournalEntry, type JournalEntryApp, type JournalEntryFile, type List, ListsResource, type LogoChurnResponse, MantleAPIError, MantleAuthenticationError, MantleCoreClient, type MantleCoreClientConfig, MantleNotFoundError, MantlePermissionError, MantleRateLimitError, MantleValidationError, MeResource, type Meeting, type MeetingAttendee, type MeetingAttendeeRef, type MeetingContactRef, type MeetingCustomer, type MeetingDeal, type MeetingPermission, type MeetingTaskSuggestion, type MeetingTranscript, type MeetingUser, type MeetingUtterance, MeetingsResource, MetricsResource, type MissingScopesError, type MrrResponse, type NetInstallsResponse, type NetRevenueResponse, type NetRevenueRetentionResponse, type Organization, OrganizationResource, type Pagination, type PayoutResponse, type Plan, type PlanFeature, type PlanFeatureWebhook, type PlanUsageChargeWebhook, type PlanWebhook, type PredictedLtvResponse, type RateLimitOptions, type RevenueRetentionResponse, type Review, type ReviewWebhook, type StandardMetricResponse, type Subscription, type SubscriptionWebhook, SubscriptionsResource, type SyncedEmail, type SyncedEmailContact, type SyncedEmailCustomer, type SyncedEmailDeal, type SyncedEmailMessage, type SyncedEmailUser, SyncedEmailsResource, type Task, type TaskComment, type TaskTodoItem, TasksResource, type ThreadEventType, type ThreadMessageWebhook, type ThreadWebhook, type Ticket, type TicketAgent, type TicketApp, type TicketAttachment, type TicketChannel, type TicketContact, type TicketCustomer, type TicketEvent, type TicketLoop, type TicketMessage, TicketsResource, type TimeSeriesDataPoint, type TimelineComment, TimelineCommentsResource, type Transaction, type TransactionWebhook, TransactionsResource, type Trigger, type UninstallEventWebhook, type UsageEvent, type UsageEventResponse, UsageEventsResource, type UsageMetric, type UsageMetricResponse, type User, UsersResource, type Webhook, WebhooksResource, type components, createRateLimitMiddleware, type paths };
|
package/dist/index.js
CHANGED
|
@@ -76,7 +76,6 @@ __export(index_exports, {
|
|
|
76
76
|
UsageEventsResource: () => UsageEventsResource,
|
|
77
77
|
UsersResource: () => UsersResource,
|
|
78
78
|
WebhooksResource: () => WebhooksResource,
|
|
79
|
-
createAuthRefreshMiddleware: () => createAuthRefreshMiddleware,
|
|
80
79
|
createRateLimitMiddleware: () => createRateLimitMiddleware
|
|
81
80
|
});
|
|
82
81
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -98,9 +97,10 @@ var MantleAPIError = class _MantleAPIError extends Error {
|
|
|
98
97
|
}
|
|
99
98
|
};
|
|
100
99
|
var MantleAuthenticationError = class _MantleAuthenticationError extends MantleAPIError {
|
|
101
|
-
constructor(message = "Authentication failed") {
|
|
100
|
+
constructor(message = "Authentication failed", isExpired = false) {
|
|
102
101
|
super(message, 401);
|
|
103
102
|
this.name = "MantleAuthenticationError";
|
|
103
|
+
this.isExpired = isExpired;
|
|
104
104
|
Object.setPrototypeOf(this, _MantleAuthenticationError.prototype);
|
|
105
105
|
}
|
|
106
106
|
};
|
|
@@ -166,7 +166,10 @@ var BaseResource = class {
|
|
|
166
166
|
const details = typeof err?.details === "string" ? err.details : void 0;
|
|
167
167
|
switch (response.status) {
|
|
168
168
|
case 401:
|
|
169
|
-
return new MantleAuthenticationError(
|
|
169
|
+
return new MantleAuthenticationError(
|
|
170
|
+
message,
|
|
171
|
+
message.startsWith("token_expired")
|
|
172
|
+
);
|
|
170
173
|
case 403:
|
|
171
174
|
return new MantlePermissionError(message);
|
|
172
175
|
case 404:
|
|
@@ -1171,7 +1174,7 @@ var MantleCoreClient = class {
|
|
|
1171
1174
|
...config.fetch ? { fetch: config.fetch } : {}
|
|
1172
1175
|
});
|
|
1173
1176
|
this._api.use({
|
|
1174
|
-
onRequest: ({ request }) => {
|
|
1177
|
+
onRequest: async ({ request }) => {
|
|
1175
1178
|
const token = this.accessToken || this.apiKey;
|
|
1176
1179
|
if (token) {
|
|
1177
1180
|
request.headers.set("Authorization", `Bearer ${token}`);
|
|
@@ -1251,7 +1254,7 @@ var MantleCoreClient = class {
|
|
|
1251
1254
|
return this;
|
|
1252
1255
|
}
|
|
1253
1256
|
/**
|
|
1254
|
-
* Update authentication credentials
|
|
1257
|
+
* Update authentication credentials at runtime.
|
|
1255
1258
|
*/
|
|
1256
1259
|
updateAuth(credentials) {
|
|
1257
1260
|
if (credentials.apiKey !== void 0) {
|
|
@@ -1263,46 +1266,6 @@ var MantleCoreClient = class {
|
|
|
1263
1266
|
}
|
|
1264
1267
|
};
|
|
1265
1268
|
|
|
1266
|
-
// src/middleware/auth-refresh.ts
|
|
1267
|
-
function createAuthRefreshMiddleware(options) {
|
|
1268
|
-
const {
|
|
1269
|
-
refreshToken,
|
|
1270
|
-
updateAuth,
|
|
1271
|
-
onRefreshSuccess,
|
|
1272
|
-
onRefreshFailed,
|
|
1273
|
-
maxRefreshAttempts = 1
|
|
1274
|
-
} = options;
|
|
1275
|
-
let refreshAttempts = 0;
|
|
1276
|
-
return {
|
|
1277
|
-
async onResponse({ request, response }) {
|
|
1278
|
-
if (response.status !== 401) return void 0;
|
|
1279
|
-
if (refreshAttempts >= maxRefreshAttempts) {
|
|
1280
|
-
refreshAttempts = 0;
|
|
1281
|
-
return void 0;
|
|
1282
|
-
}
|
|
1283
|
-
refreshAttempts++;
|
|
1284
|
-
try {
|
|
1285
|
-
const newToken = await refreshToken();
|
|
1286
|
-
updateAuth(newToken);
|
|
1287
|
-
onRefreshSuccess?.(newToken);
|
|
1288
|
-
refreshAttempts = 0;
|
|
1289
|
-
const headers = new Headers(request.headers);
|
|
1290
|
-
headers.set("Authorization", `Bearer ${newToken}`);
|
|
1291
|
-
return fetch(new Request(request.url, {
|
|
1292
|
-
method: request.method,
|
|
1293
|
-
headers,
|
|
1294
|
-
body: request.body,
|
|
1295
|
-
signal: request.signal
|
|
1296
|
-
}));
|
|
1297
|
-
} catch (refreshError) {
|
|
1298
|
-
refreshAttempts = 0;
|
|
1299
|
-
onRefreshFailed?.(refreshError);
|
|
1300
|
-
return void 0;
|
|
1301
|
-
}
|
|
1302
|
-
}
|
|
1303
|
-
};
|
|
1304
|
-
}
|
|
1305
|
-
|
|
1306
1269
|
// src/middleware/rate-limit.ts
|
|
1307
1270
|
var RateLimiter = class {
|
|
1308
1271
|
constructor(options) {
|
|
@@ -1468,6 +1431,5 @@ function createRateLimitMiddleware(options = {}) {
|
|
|
1468
1431
|
UsageEventsResource,
|
|
1469
1432
|
UsersResource,
|
|
1470
1433
|
WebhooksResource,
|
|
1471
|
-
createAuthRefreshMiddleware,
|
|
1472
1434
|
createRateLimitMiddleware
|
|
1473
1435
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -15,9 +15,10 @@ var MantleAPIError = class _MantleAPIError extends Error {
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
var MantleAuthenticationError = class _MantleAuthenticationError extends MantleAPIError {
|
|
18
|
-
constructor(message = "Authentication failed") {
|
|
18
|
+
constructor(message = "Authentication failed", isExpired = false) {
|
|
19
19
|
super(message, 401);
|
|
20
20
|
this.name = "MantleAuthenticationError";
|
|
21
|
+
this.isExpired = isExpired;
|
|
21
22
|
Object.setPrototypeOf(this, _MantleAuthenticationError.prototype);
|
|
22
23
|
}
|
|
23
24
|
};
|
|
@@ -83,7 +84,10 @@ var BaseResource = class {
|
|
|
83
84
|
const details = typeof err?.details === "string" ? err.details : void 0;
|
|
84
85
|
switch (response.status) {
|
|
85
86
|
case 401:
|
|
86
|
-
return new MantleAuthenticationError(
|
|
87
|
+
return new MantleAuthenticationError(
|
|
88
|
+
message,
|
|
89
|
+
message.startsWith("token_expired")
|
|
90
|
+
);
|
|
87
91
|
case 403:
|
|
88
92
|
return new MantlePermissionError(message);
|
|
89
93
|
case 404:
|
|
@@ -1088,7 +1092,7 @@ var MantleCoreClient = class {
|
|
|
1088
1092
|
...config.fetch ? { fetch: config.fetch } : {}
|
|
1089
1093
|
});
|
|
1090
1094
|
this._api.use({
|
|
1091
|
-
onRequest: ({ request }) => {
|
|
1095
|
+
onRequest: async ({ request }) => {
|
|
1092
1096
|
const token = this.accessToken || this.apiKey;
|
|
1093
1097
|
if (token) {
|
|
1094
1098
|
request.headers.set("Authorization", `Bearer ${token}`);
|
|
@@ -1168,7 +1172,7 @@ var MantleCoreClient = class {
|
|
|
1168
1172
|
return this;
|
|
1169
1173
|
}
|
|
1170
1174
|
/**
|
|
1171
|
-
* Update authentication credentials
|
|
1175
|
+
* Update authentication credentials at runtime.
|
|
1172
1176
|
*/
|
|
1173
1177
|
updateAuth(credentials) {
|
|
1174
1178
|
if (credentials.apiKey !== void 0) {
|
|
@@ -1180,46 +1184,6 @@ var MantleCoreClient = class {
|
|
|
1180
1184
|
}
|
|
1181
1185
|
};
|
|
1182
1186
|
|
|
1183
|
-
// src/middleware/auth-refresh.ts
|
|
1184
|
-
function createAuthRefreshMiddleware(options) {
|
|
1185
|
-
const {
|
|
1186
|
-
refreshToken,
|
|
1187
|
-
updateAuth,
|
|
1188
|
-
onRefreshSuccess,
|
|
1189
|
-
onRefreshFailed,
|
|
1190
|
-
maxRefreshAttempts = 1
|
|
1191
|
-
} = options;
|
|
1192
|
-
let refreshAttempts = 0;
|
|
1193
|
-
return {
|
|
1194
|
-
async onResponse({ request, response }) {
|
|
1195
|
-
if (response.status !== 401) return void 0;
|
|
1196
|
-
if (refreshAttempts >= maxRefreshAttempts) {
|
|
1197
|
-
refreshAttempts = 0;
|
|
1198
|
-
return void 0;
|
|
1199
|
-
}
|
|
1200
|
-
refreshAttempts++;
|
|
1201
|
-
try {
|
|
1202
|
-
const newToken = await refreshToken();
|
|
1203
|
-
updateAuth(newToken);
|
|
1204
|
-
onRefreshSuccess?.(newToken);
|
|
1205
|
-
refreshAttempts = 0;
|
|
1206
|
-
const headers = new Headers(request.headers);
|
|
1207
|
-
headers.set("Authorization", `Bearer ${newToken}`);
|
|
1208
|
-
return fetch(new Request(request.url, {
|
|
1209
|
-
method: request.method,
|
|
1210
|
-
headers,
|
|
1211
|
-
body: request.body,
|
|
1212
|
-
signal: request.signal
|
|
1213
|
-
}));
|
|
1214
|
-
} catch (refreshError) {
|
|
1215
|
-
refreshAttempts = 0;
|
|
1216
|
-
onRefreshFailed?.(refreshError);
|
|
1217
|
-
return void 0;
|
|
1218
|
-
}
|
|
1219
|
-
}
|
|
1220
|
-
};
|
|
1221
|
-
}
|
|
1222
|
-
|
|
1223
1187
|
// src/middleware/rate-limit.ts
|
|
1224
1188
|
var RateLimiter = class {
|
|
1225
1189
|
constructor(options) {
|
|
@@ -1384,6 +1348,5 @@ export {
|
|
|
1384
1348
|
UsageEventsResource,
|
|
1385
1349
|
UsersResource,
|
|
1386
1350
|
WebhooksResource,
|
|
1387
|
-
createAuthRefreshMiddleware,
|
|
1388
1351
|
createRateLimitMiddleware
|
|
1389
1352
|
};
|