stripe-loaders-core 0.1.0 → 0.3.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.
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Rate limiting and retry helpers for fetching Stripe sites.
3
+ *
4
+ * Requests are sent one at a time with a delay between them, and HTTP 429 / 5xx
5
+ * responses are retried with exponential backoff (honouring `Retry-After`).
6
+ * The defaults follow the conservative access policy used by
7
+ * hideokamoto/stripe-docs-updates for docs.stripe.com and stripe.com.
8
+ */
9
+ interface RateLimitOptions {
10
+ /** Delay between consecutive requests in milliseconds. */
11
+ requestDelayMs: number;
12
+ /** Random variation (±half of this value) added to each delay in milliseconds. */
13
+ requestJitterMs: number;
14
+ /** Number of retries after the first attempt for 429 / 5xx responses and network errors. */
15
+ maxRetries: number;
16
+ /** Base delay for exponential backoff in milliseconds. */
17
+ backoffBaseMs: number;
18
+ /** Upper bound for a single backoff delay in milliseconds. */
19
+ backoffMaxMs: number;
20
+ }
21
+ declare const DEFAULT_RATE_LIMIT: Readonly<RateLimitOptions>;
22
+ type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
23
+ type SleepFn = (ms: number) => Promise<void>;
24
+ declare function resolveRateLimit(options?: Partial<RateLimitOptions>): RateLimitOptions;
25
+ declare function delay(ms: number): Promise<void>;
26
+ declare function withJitter(baseMs: number, jitterMs: number, random?: () => number): number;
27
+ declare function delayWithJitter(baseMs: number, jitterMs: number, sleep?: SleepFn): Promise<void>;
28
+ declare function calculateExponentialBackoff(attempt: number, baseMs: number, maxMs: number): number;
29
+ /**
30
+ * Parse a `Retry-After` header (seconds or HTTP date) into milliseconds.
31
+ * Returns undefined when the header is missing, invalid, or not in the future.
32
+ */
33
+ declare function parseRetryAfter(value: string | null | undefined, now?: number): number | undefined;
34
+ interface FetchWithRetryOptions {
35
+ rateLimit?: Partial<RateLimitOptions>;
36
+ fetch?: FetchLike;
37
+ sleep?: SleepFn;
38
+ /** Called before each retry; useful for logging. */
39
+ onRetry?: (info: {
40
+ url: string;
41
+ attempt: number;
42
+ waitMs: number;
43
+ reason: string;
44
+ }) => void;
45
+ }
46
+ /**
47
+ * Fetch a URL, retrying 429 / 5xx responses and network errors.
48
+ *
49
+ * The final response is returned as-is, including non-2xx responses, so the
50
+ * caller decides how to treat 404 and similar statuses. A network error is
51
+ * rethrown after the last retry.
52
+ */
53
+ declare function fetchWithRetry(url: string, init?: RequestInit, options?: FetchWithRetryOptions): Promise<Response>;
54
+ /**
55
+ * Sequential request pacer: waits `requestDelayMs` (± jitter) before every
56
+ * request except the first one.
57
+ */
58
+ declare class RequestPacer {
59
+ private readonly limits;
60
+ private readonly sleep;
61
+ private started;
62
+ constructor(limits: RateLimitOptions, sleep?: SleepFn);
63
+ wait(): Promise<void>;
64
+ }
65
+
66
+ /**
67
+ * Parser and fetcher for the Stripe API changelog (https://docs.stripe.com/changelog.md).
68
+ *
69
+ * The Markdown is organised as:
70
+ * # <release> (e.g. `# Endive`, or a year such as `# 2024` for older entries)
71
+ * ## <version> (e.g. `## 2026-11-18.endive`, `## 2026-11-18.preview`, `## 2011-06-21`)
72
+ * ### <category> (e.g. `### Payments`; localised when `?locale=` is set)
73
+ * | Title | Affected Products | Breaking change? | Category |
74
+ * | [title](url) | Connect, Payments | Breaking / Non-breaking | api |
75
+ */
76
+
77
+ declare const STRIPE_CHANGELOG_URL = "https://docs.stripe.com/changelog.md";
78
+ interface StripeChangelogEntry {
79
+ /** Release the version belongs to (e.g. `Endive`, or `2024` for year-grouped entries). */
80
+ release?: string;
81
+ /** Version heading as written (e.g. `2026-11-18.endive`). */
82
+ version: string;
83
+ /** `YYYY-MM-DD` taken from the version, or an empty string when the version has no date. */
84
+ date: string;
85
+ /** Section heading inside the version (e.g. `Payments`). */
86
+ category?: string;
87
+ title: string;
88
+ /** Absolute URL of the entry's detail page (Markdown), or an empty string when the row has no link. */
89
+ link: string;
90
+ affectedProducts: string[];
91
+ isBreakingChange: boolean;
92
+ /** Value of the `Category` column (e.g. `api`, `stripejs`). */
93
+ changeType?: string;
94
+ }
95
+ interface StripeChangelogVersion {
96
+ release?: string;
97
+ version: string;
98
+ date: string;
99
+ entries: StripeChangelogEntry[];
100
+ }
101
+ /**
102
+ * Parse the changelog Markdown into versions, newest first (the order of the document).
103
+ * Versions without any entries are omitted.
104
+ *
105
+ * @param markdown Changelog Markdown
106
+ * @param baseUrl Base used to resolve relative links (defaults to https://docs.stripe.com/)
107
+ */
108
+ declare function parseStripeChangelog(markdown: string, baseUrl?: string): StripeChangelogVersion[];
109
+ interface FetchStripeChangelogOptions {
110
+ /** Locale for the changelog (e.g. `ja-JP`). Omitted: the default (English) changelog. */
111
+ locale?: string;
112
+ /** Changelog URL. Defaults to https://docs.stripe.com/changelog.md */
113
+ url?: string;
114
+ rateLimit?: Partial<RateLimitOptions>;
115
+ fetch?: FetchLike;
116
+ sleep?: SleepFn;
117
+ }
118
+ declare function stripeChangelogUrl(options?: Pick<FetchStripeChangelogOptions, 'locale' | 'url'>): string;
119
+ /**
120
+ * Fetch and parse the Stripe API changelog (one request, retried on 429 / 5xx).
121
+ * Throws when the final response is not 2xx.
122
+ */
123
+ declare function fetchStripeChangelog(options?: FetchStripeChangelogOptions): Promise<StripeChangelogVersion[]>;
124
+
125
+ /**
126
+ * Utility functions for processing Stripe docs sitemaps
127
+ */
128
+
129
+ /**
130
+ * Escape a string so it can be embedded in a regular expression literally.
131
+ */
132
+ declare function escapeRegExp(value: string): string;
133
+ interface SitemapProcessorOptions {
134
+ debug?: boolean;
135
+ logger?: Logger;
136
+ /** Rate limiting and retry settings for sitemap requests. */
137
+ rateLimit?: Partial<RateLimitOptions>;
138
+ /** Custom fetch implementation (defaults to the global fetch). */
139
+ fetch?: FetchLike;
140
+ /** Custom sleep implementation, mainly for tests. */
141
+ sleep?: SleepFn;
142
+ }
143
+ /**
144
+ * Logger interface for SitemapProcessor
145
+ */
146
+ interface Logger {
147
+ log(message: string): void;
148
+ error(message: string): void;
149
+ warn(message: string): void;
150
+ }
151
+ /**
152
+ * Default console logger implementation
153
+ */
154
+ declare class ConsoleLogger implements Logger {
155
+ private debug;
156
+ constructor(debug?: boolean);
157
+ log(message: string): void;
158
+ error(message: string): void;
159
+ warn(message: string): void;
160
+ }
161
+ /**
162
+ * Utility class for processing Stripe docs sitemaps
163
+ */
164
+ declare class SitemapProcessor {
165
+ private debug;
166
+ private logger;
167
+ private rateLimit;
168
+ private fetchImpl?;
169
+ private sleep?;
170
+ /**
171
+ * Constructor for SitemapProcessor
172
+ * @param options Configuration options
173
+ */
174
+ constructor(options?: SitemapProcessorOptions);
175
+ private fetchUrl;
176
+ /**
177
+ * Log a message if debug mode is enabled
178
+ * @param message Message to log
179
+ */
180
+ private log;
181
+ /**
182
+ * Log an error message
183
+ * @param message Error message to log
184
+ */
185
+ private logError;
186
+ /**
187
+ * Log a warning message
188
+ * @param message Warning message to log
189
+ */
190
+ private logWarning;
191
+ /**
192
+ * Extract URLs from a sitemap
193
+ * @param content Sitemap XML content
194
+ * @param baseUrl Base URL part to extract (e.g., https://docs.stripe.com/)
195
+ * @returns Array of extracted URLs
196
+ */
197
+ extractUrlsFromSitemap(content: string, baseUrl?: string): string[];
198
+ /**
199
+ * Extract sitemap URLs from a sitemap index
200
+ * @param content Sitemap index XML content
201
+ * @returns Array of extracted sitemap URLs
202
+ */
203
+ extractSitemapUrlsFromIndex(content: string): string[];
204
+ /**
205
+ * Find newly added URLs
206
+ * @param currentUrls Current URL list
207
+ * @param previousUrls Previous URL list
208
+ * @returns Array of newly added URLs
209
+ */
210
+ findNewUrls(currentUrls: string[], previousUrls: string[]): string[];
211
+ /**
212
+ * Fetch and process a sitemap
213
+ * @param url Sitemap URL
214
+ * @param baseUrl Base URL part to extract
215
+ * @returns List of retrieved URLs
216
+ */
217
+ fetchAndProcessSitemap(url: string, baseUrl?: string): Promise<string[]>;
218
+ /**
219
+ * Fetch and process a sitemap index
220
+ * @param indexUrl Sitemap index URL
221
+ * @param baseUrl Base URL part to extract
222
+ * @returns List of retrieved URLs
223
+ */
224
+ fetchAndProcessSitemapIndex(indexUrl: string, baseUrl?: string): Promise<string[]>;
225
+ }
226
+
227
+ export { ConsoleLogger, DEFAULT_RATE_LIMIT, type FetchLike, type FetchStripeChangelogOptions, type FetchWithRetryOptions, type Logger, type RateLimitOptions, RequestPacer, STRIPE_CHANGELOG_URL, SitemapProcessor, type SitemapProcessorOptions, type SleepFn, type StripeChangelogEntry, type StripeChangelogVersion, calculateExponentialBackoff, delay, delayWithJitter, escapeRegExp, fetchStripeChangelog, fetchWithRetry, parseRetryAfter, parseStripeChangelog, resolveRateLimit, stripeChangelogUrl, withJitter };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,149 @@
1
+ /**
2
+ * Rate limiting and retry helpers for fetching Stripe sites.
3
+ *
4
+ * Requests are sent one at a time with a delay between them, and HTTP 429 / 5xx
5
+ * responses are retried with exponential backoff (honouring `Retry-After`).
6
+ * The defaults follow the conservative access policy used by
7
+ * hideokamoto/stripe-docs-updates for docs.stripe.com and stripe.com.
8
+ */
9
+ interface RateLimitOptions {
10
+ /** Delay between consecutive requests in milliseconds. */
11
+ requestDelayMs: number;
12
+ /** Random variation (±half of this value) added to each delay in milliseconds. */
13
+ requestJitterMs: number;
14
+ /** Number of retries after the first attempt for 429 / 5xx responses and network errors. */
15
+ maxRetries: number;
16
+ /** Base delay for exponential backoff in milliseconds. */
17
+ backoffBaseMs: number;
18
+ /** Upper bound for a single backoff delay in milliseconds. */
19
+ backoffMaxMs: number;
20
+ }
21
+ declare const DEFAULT_RATE_LIMIT: Readonly<RateLimitOptions>;
22
+ type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
23
+ type SleepFn = (ms: number) => Promise<void>;
24
+ declare function resolveRateLimit(options?: Partial<RateLimitOptions>): RateLimitOptions;
25
+ declare function delay(ms: number): Promise<void>;
26
+ declare function withJitter(baseMs: number, jitterMs: number, random?: () => number): number;
27
+ declare function delayWithJitter(baseMs: number, jitterMs: number, sleep?: SleepFn): Promise<void>;
28
+ declare function calculateExponentialBackoff(attempt: number, baseMs: number, maxMs: number): number;
29
+ /**
30
+ * Parse a `Retry-After` header (seconds or HTTP date) into milliseconds.
31
+ * Returns undefined when the header is missing, invalid, or not in the future.
32
+ */
33
+ declare function parseRetryAfter(value: string | null | undefined, now?: number): number | undefined;
34
+ interface FetchWithRetryOptions {
35
+ rateLimit?: Partial<RateLimitOptions>;
36
+ fetch?: FetchLike;
37
+ sleep?: SleepFn;
38
+ /** Called before each retry; useful for logging. */
39
+ onRetry?: (info: {
40
+ url: string;
41
+ attempt: number;
42
+ waitMs: number;
43
+ reason: string;
44
+ }) => void;
45
+ }
46
+ /**
47
+ * Fetch a URL, retrying 429 / 5xx responses and network errors.
48
+ *
49
+ * The final response is returned as-is, including non-2xx responses, so the
50
+ * caller decides how to treat 404 and similar statuses. A network error is
51
+ * rethrown after the last retry.
52
+ */
53
+ declare function fetchWithRetry(url: string, init?: RequestInit, options?: FetchWithRetryOptions): Promise<Response>;
54
+ /**
55
+ * Sequential request pacer: waits `requestDelayMs` (± jitter) before every
56
+ * request except the first one.
57
+ */
58
+ declare class RequestPacer {
59
+ private readonly limits;
60
+ private readonly sleep;
61
+ private started;
62
+ constructor(limits: RateLimitOptions, sleep?: SleepFn);
63
+ wait(): Promise<void>;
64
+ }
65
+
66
+ /**
67
+ * Parser and fetcher for the Stripe API changelog (https://docs.stripe.com/changelog.md).
68
+ *
69
+ * The Markdown is organised as:
70
+ * # <release> (e.g. `# Endive`, or a year such as `# 2024` for older entries)
71
+ * ## <version> (e.g. `## 2026-11-18.endive`, `## 2026-11-18.preview`, `## 2011-06-21`)
72
+ * ### <category> (e.g. `### Payments`; localised when `?locale=` is set)
73
+ * | Title | Affected Products | Breaking change? | Category |
74
+ * | [title](url) | Connect, Payments | Breaking / Non-breaking | api |
75
+ */
76
+
77
+ declare const STRIPE_CHANGELOG_URL = "https://docs.stripe.com/changelog.md";
78
+ interface StripeChangelogEntry {
79
+ /** Release the version belongs to (e.g. `Endive`, or `2024` for year-grouped entries). */
80
+ release?: string;
81
+ /** Version heading as written (e.g. `2026-11-18.endive`). */
82
+ version: string;
83
+ /** `YYYY-MM-DD` taken from the version, or an empty string when the version has no date. */
84
+ date: string;
85
+ /** Section heading inside the version (e.g. `Payments`). */
86
+ category?: string;
87
+ title: string;
88
+ /** Absolute URL of the entry's detail page (Markdown), or an empty string when the row has no link. */
89
+ link: string;
90
+ affectedProducts: string[];
91
+ isBreakingChange: boolean;
92
+ /** Value of the `Category` column (e.g. `api`, `stripejs`). */
93
+ changeType?: string;
94
+ }
95
+ interface StripeChangelogVersion {
96
+ release?: string;
97
+ version: string;
98
+ date: string;
99
+ entries: StripeChangelogEntry[];
100
+ }
101
+ /**
102
+ * Parse the changelog Markdown into versions, newest first (the order of the document).
103
+ * Versions without any entries are omitted.
104
+ *
105
+ * @param markdown Changelog Markdown
106
+ * @param baseUrl Base used to resolve relative links (defaults to https://docs.stripe.com/)
107
+ */
108
+ declare function parseStripeChangelog(markdown: string, baseUrl?: string): StripeChangelogVersion[];
109
+ interface FetchStripeChangelogOptions {
110
+ /** Locale for the changelog (e.g. `ja-JP`). Omitted: the default (English) changelog. */
111
+ locale?: string;
112
+ /** Changelog URL. Defaults to https://docs.stripe.com/changelog.md */
113
+ url?: string;
114
+ rateLimit?: Partial<RateLimitOptions>;
115
+ fetch?: FetchLike;
116
+ sleep?: SleepFn;
117
+ }
118
+ declare function stripeChangelogUrl(options?: Pick<FetchStripeChangelogOptions, 'locale' | 'url'>): string;
119
+ /**
120
+ * Fetch and parse the Stripe API changelog (one request, retried on 429 / 5xx).
121
+ * Throws when the final response is not 2xx.
122
+ */
123
+ declare function fetchStripeChangelog(options?: FetchStripeChangelogOptions): Promise<StripeChangelogVersion[]>;
124
+
1
125
  /**
2
126
  * Utility functions for processing Stripe docs sitemaps
3
127
  */
128
+
129
+ /**
130
+ * Escape a string so it can be embedded in a regular expression literally.
131
+ */
132
+ declare function escapeRegExp(value: string): string;
133
+ interface SitemapProcessorOptions {
134
+ debug?: boolean;
135
+ logger?: Logger;
136
+ /** Rate limiting and retry settings for sitemap requests. */
137
+ rateLimit?: Partial<RateLimitOptions>;
138
+ /** Custom fetch implementation (defaults to the global fetch). */
139
+ fetch?: FetchLike;
140
+ /** Custom sleep implementation, mainly for tests. */
141
+ sleep?: SleepFn;
142
+ }
4
143
  /**
5
144
  * Logger interface for SitemapProcessor
6
145
  */
7
- export interface Logger {
146
+ interface Logger {
8
147
  log(message: string): void;
9
148
  error(message: string): void;
10
149
  warn(message: string): void;
@@ -12,7 +151,7 @@ export interface Logger {
12
151
  /**
13
152
  * Default console logger implementation
14
153
  */
15
- export declare class ConsoleLogger implements Logger {
154
+ declare class ConsoleLogger implements Logger {
16
155
  private debug;
17
156
  constructor(debug?: boolean);
18
157
  log(message: string): void;
@@ -22,17 +161,18 @@ export declare class ConsoleLogger implements Logger {
22
161
  /**
23
162
  * Utility class for processing Stripe docs sitemaps
24
163
  */
25
- export declare class SitemapProcessor {
164
+ declare class SitemapProcessor {
26
165
  private debug;
27
166
  private logger;
167
+ private rateLimit;
168
+ private fetchImpl?;
169
+ private sleep?;
28
170
  /**
29
171
  * Constructor for SitemapProcessor
30
172
  * @param options Configuration options
31
173
  */
32
- constructor(options?: {
33
- debug?: boolean;
34
- logger?: Logger;
35
- });
174
+ constructor(options?: SitemapProcessorOptions);
175
+ private fetchUrl;
36
176
  /**
37
177
  * Log a message if debug mode is enabled
38
178
  * @param message Message to log
@@ -83,4 +223,5 @@ export declare class SitemapProcessor {
83
223
  */
84
224
  fetchAndProcessSitemapIndex(indexUrl: string, baseUrl?: string): Promise<string[]>;
85
225
  }
86
- //# sourceMappingURL=index.d.ts.map
226
+
227
+ export { ConsoleLogger, DEFAULT_RATE_LIMIT, type FetchLike, type FetchStripeChangelogOptions, type FetchWithRetryOptions, type Logger, type RateLimitOptions, RequestPacer, STRIPE_CHANGELOG_URL, SitemapProcessor, type SitemapProcessorOptions, type SleepFn, type StripeChangelogEntry, type StripeChangelogVersion, calculateExponentialBackoff, delay, delayWithJitter, escapeRegExp, fetchStripeChangelog, fetchWithRetry, parseRetryAfter, parseStripeChangelog, resolveRateLimit, stripeChangelogUrl, withJitter };