checkout-intents 0.4.0 → 0.6.1

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.
Files changed (112) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +88 -14
  3. package/client.d.mts +9 -6
  4. package/client.d.mts.map +1 -1
  5. package/client.d.ts +9 -6
  6. package/client.d.ts.map +1 -1
  7. package/client.js +10 -15
  8. package/client.js.map +1 -1
  9. package/client.mjs +10 -15
  10. package/client.mjs.map +1 -1
  11. package/core/pagination.d.mts +63 -0
  12. package/core/pagination.d.mts.map +1 -0
  13. package/core/pagination.d.ts +63 -0
  14. package/core/pagination.d.ts.map +1 -0
  15. package/core/pagination.js +115 -0
  16. package/core/pagination.js.map +1 -0
  17. package/core/pagination.mjs +109 -0
  18. package/core/pagination.mjs.map +1 -0
  19. package/index.d.mts +1 -0
  20. package/index.d.mts.map +1 -1
  21. package/index.d.ts +1 -0
  22. package/index.d.ts.map +1 -1
  23. package/index.js +3 -1
  24. package/index.js.map +1 -1
  25. package/index.mjs +1 -0
  26. package/index.mjs.map +1 -1
  27. package/internal/qs/formats.d.mts +7 -0
  28. package/internal/qs/formats.d.mts.map +1 -0
  29. package/internal/qs/formats.d.ts +7 -0
  30. package/internal/qs/formats.d.ts.map +1 -0
  31. package/internal/qs/formats.js +13 -0
  32. package/internal/qs/formats.js.map +1 -0
  33. package/internal/qs/formats.mjs +9 -0
  34. package/internal/qs/formats.mjs.map +1 -0
  35. package/internal/qs/index.d.mts +10 -0
  36. package/internal/qs/index.d.mts.map +1 -0
  37. package/internal/qs/index.d.ts +10 -0
  38. package/internal/qs/index.d.ts.map +1 -0
  39. package/internal/qs/index.js +14 -0
  40. package/internal/qs/index.js.map +1 -0
  41. package/internal/qs/index.mjs +10 -0
  42. package/internal/qs/index.mjs.map +1 -0
  43. package/internal/qs/stringify.d.mts +3 -0
  44. package/internal/qs/stringify.d.mts.map +1 -0
  45. package/internal/qs/stringify.d.ts +3 -0
  46. package/internal/qs/stringify.d.ts.map +1 -0
  47. package/internal/qs/stringify.js +277 -0
  48. package/internal/qs/stringify.js.map +1 -0
  49. package/internal/qs/stringify.mjs +274 -0
  50. package/internal/qs/stringify.mjs.map +1 -0
  51. package/internal/qs/types.d.mts +57 -0
  52. package/internal/qs/types.d.mts.map +1 -0
  53. package/internal/qs/types.d.ts +57 -0
  54. package/internal/qs/types.d.ts.map +1 -0
  55. package/internal/qs/types.js +3 -0
  56. package/internal/qs/types.js.map +1 -0
  57. package/internal/qs/types.mjs +2 -0
  58. package/internal/qs/types.mjs.map +1 -0
  59. package/internal/qs/utils.d.mts +15 -0
  60. package/internal/qs/utils.d.mts.map +1 -0
  61. package/internal/qs/utils.d.ts +15 -0
  62. package/internal/qs/utils.d.ts.map +1 -0
  63. package/internal/qs/utils.js +230 -0
  64. package/internal/qs/utils.js.map +1 -0
  65. package/internal/qs/utils.mjs +217 -0
  66. package/internal/qs/utils.mjs.map +1 -0
  67. package/package.json +11 -1
  68. package/pagination.d.mts +2 -0
  69. package/pagination.d.mts.map +1 -0
  70. package/pagination.d.ts +2 -0
  71. package/pagination.d.ts.map +1 -0
  72. package/pagination.js +6 -0
  73. package/pagination.js.map +1 -0
  74. package/pagination.mjs +2 -0
  75. package/pagination.mjs.map +1 -0
  76. package/resources/brands.d.mts +1 -1
  77. package/resources/brands.d.mts.map +1 -1
  78. package/resources/brands.d.ts +1 -1
  79. package/resources/brands.d.ts.map +1 -1
  80. package/resources/checkout-intents.d.mts +35 -6
  81. package/resources/checkout-intents.d.mts.map +1 -1
  82. package/resources/checkout-intents.d.ts +35 -6
  83. package/resources/checkout-intents.d.ts.map +1 -1
  84. package/resources/checkout-intents.js +25 -4
  85. package/resources/checkout-intents.js.map +1 -1
  86. package/resources/checkout-intents.mjs +25 -4
  87. package/resources/checkout-intents.mjs.map +1 -1
  88. package/resources/index.d.mts +1 -1
  89. package/resources/index.d.mts.map +1 -1
  90. package/resources/index.d.ts +1 -1
  91. package/resources/index.d.ts.map +1 -1
  92. package/resources/index.js.map +1 -1
  93. package/resources/index.mjs.map +1 -1
  94. package/src/client.ts +34 -18
  95. package/src/core/pagination.ts +187 -0
  96. package/src/index.ts +1 -0
  97. package/src/internal/qs/LICENSE.md +13 -0
  98. package/src/internal/qs/README.md +3 -0
  99. package/src/internal/qs/formats.ts +10 -0
  100. package/src/internal/qs/index.ts +13 -0
  101. package/src/internal/qs/stringify.ts +385 -0
  102. package/src/internal/qs/types.ts +71 -0
  103. package/src/internal/qs/utils.ts +265 -0
  104. package/src/pagination.ts +2 -0
  105. package/src/resources/brands.ts +1 -1
  106. package/src/resources/checkout-intents.ts +62 -5
  107. package/src/resources/index.ts +2 -0
  108. package/src/version.ts +1 -1
  109. package/version.d.mts +1 -1
  110. package/version.d.ts +1 -1
  111. package/version.js +1 -1
  112. package/version.mjs +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,MAAM,EAA8B;OACtC,EACL,uBAAuB,GAWxB"}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/resources/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,MAAM,EAA8B;OACtC,EACL,uBAAuB,GAaxB"}
package/src/client.ts CHANGED
@@ -11,8 +11,11 @@ import type { APIResponseProps } from './internal/parse';
11
11
  import { getPlatformHeaders } from './internal/detect-platform';
12
12
  import * as Shims from './internal/shims';
13
13
  import * as Opts from './internal/request-options';
14
+ import * as qs from './internal/qs';
14
15
  import { VERSION } from './version';
15
16
  import * as Errors from './core/error';
17
+ import * as Pagination from './core/pagination';
18
+ import { AbstractPage, type CursorPaginationParams, CursorPaginationResponse } from './core/pagination';
16
19
  import * as Uploads from './core/uploads';
17
20
  import * as API from './resources/index';
18
21
  import { APIPromise } from './core/api-promise';
@@ -24,6 +27,8 @@ import {
24
27
  CheckoutIntentAddPaymentParams,
25
28
  CheckoutIntentConfirmParams,
26
29
  CheckoutIntentCreateParams,
30
+ CheckoutIntentListParams,
31
+ CheckoutIntentsCursorPagination,
27
32
  CheckoutIntentsResource,
28
33
  Money,
29
34
  Offer,
@@ -153,7 +158,7 @@ export class CheckoutIntents {
153
158
  baseURL: string;
154
159
  maxRetries: number;
155
160
  timeout: number;
156
- logger: Logger | undefined;
161
+ logger: Logger;
157
162
  logLevel: LogLevel | undefined;
158
163
  fetchOptions: MergedRequestInit | undefined;
159
164
 
@@ -269,24 +274,8 @@ export class CheckoutIntents {
269
274
  return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
270
275
  }
271
276
 
272
- /**
273
- * Basic re-implementation of `qs.stringify` for primitive types.
274
- */
275
277
  protected stringifyQuery(query: Record<string, unknown>): string {
276
- return Object.entries(query)
277
- .filter(([_, value]) => typeof value !== 'undefined')
278
- .map(([key, value]) => {
279
- if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
280
- return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
281
- }
282
- if (value === null) {
283
- return `${encodeURIComponent(key)}=`;
284
- }
285
- throw new Errors.CheckoutIntentsError(
286
- `Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`,
287
- );
288
- })
289
- .join('&');
278
+ return qs.stringify(query, { arrayFormat: 'comma' });
290
279
  }
291
280
 
292
281
  private getUserAgent(): string {
@@ -541,6 +530,25 @@ export class CheckoutIntents {
541
530
  return { response, options, controller, requestLogID, retryOfRequestLogID, startTime };
542
531
  }
543
532
 
533
+ getAPIList<Item, PageClass extends Pagination.AbstractPage<Item> = Pagination.AbstractPage<Item>>(
534
+ path: string,
535
+ Page: new (...args: any[]) => PageClass,
536
+ opts?: RequestOptions,
537
+ ): Pagination.PagePromise<PageClass, Item> {
538
+ return this.requestAPIList(Page, { method: 'get', path, ...opts });
539
+ }
540
+
541
+ requestAPIList<
542
+ Item = unknown,
543
+ PageClass extends Pagination.AbstractPage<Item> = Pagination.AbstractPage<Item>,
544
+ >(
545
+ Page: new (...args: ConstructorParameters<typeof Pagination.AbstractPage>) => PageClass,
546
+ options: FinalRequestOptions,
547
+ ): Pagination.PagePromise<PageClass, Item> {
548
+ const request = this.makeRequest(options, null, undefined);
549
+ return new Pagination.PagePromise<PageClass, Item>(this as any as CheckoutIntents, request, Page);
550
+ }
551
+
544
552
  async fetchWithTimeout(
545
553
  url: RequestInfo,
546
554
  init: RequestInit | undefined,
@@ -783,6 +791,12 @@ CheckoutIntents.Brands = Brands;
783
791
  export declare namespace CheckoutIntents {
784
792
  export type RequestOptions = Opts.RequestOptions;
785
793
 
794
+ export import CursorPagination = Pagination.CursorPagination;
795
+ export {
796
+ type CursorPaginationParams as CursorPaginationParams,
797
+ type CursorPaginationResponse as CursorPaginationResponse,
798
+ };
799
+
786
800
  export {
787
801
  CheckoutIntentsResource as CheckoutIntentsResource,
788
802
  type BaseCheckoutIntent as BaseCheckoutIntent,
@@ -792,7 +806,9 @@ export declare namespace CheckoutIntents {
792
806
  type Offer as Offer,
793
807
  type PaymentMethod as PaymentMethod,
794
808
  type VariantSelection as VariantSelection,
809
+ type CheckoutIntentsCursorPagination as CheckoutIntentsCursorPagination,
795
810
  type CheckoutIntentCreateParams as CheckoutIntentCreateParams,
811
+ type CheckoutIntentListParams as CheckoutIntentListParams,
796
812
  type CheckoutIntentAddPaymentParams as CheckoutIntentAddPaymentParams,
797
813
  type CheckoutIntentConfirmParams as CheckoutIntentConfirmParams,
798
814
  };
@@ -0,0 +1,187 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { CheckoutIntentsError } from './error';
4
+ import { FinalRequestOptions } from '../internal/request-options';
5
+ import { defaultParseResponse } from '../internal/parse';
6
+ import { type CheckoutIntents } from '../client';
7
+ import { APIPromise } from './api-promise';
8
+ import { type APIResponseProps } from '../internal/parse';
9
+ import { maybeObj } from '../internal/utils/values';
10
+
11
+ export type PageRequestOptions = Pick<FinalRequestOptions, 'query' | 'headers' | 'body' | 'path' | 'method'>;
12
+
13
+ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
14
+ #client: CheckoutIntents;
15
+ protected options: FinalRequestOptions;
16
+
17
+ protected response: Response;
18
+ protected body: unknown;
19
+
20
+ constructor(client: CheckoutIntents, response: Response, body: unknown, options: FinalRequestOptions) {
21
+ this.#client = client;
22
+ this.options = options;
23
+ this.response = response;
24
+ this.body = body;
25
+ }
26
+
27
+ abstract nextPageRequestOptions(): PageRequestOptions | null;
28
+
29
+ abstract getPaginatedItems(): Item[];
30
+
31
+ hasNextPage(): boolean {
32
+ const items = this.getPaginatedItems();
33
+ if (!items.length) return false;
34
+ return this.nextPageRequestOptions() != null;
35
+ }
36
+
37
+ async getNextPage(): Promise<this> {
38
+ const nextOptions = this.nextPageRequestOptions();
39
+ if (!nextOptions) {
40
+ throw new CheckoutIntentsError(
41
+ 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.',
42
+ );
43
+ }
44
+
45
+ return await this.#client.requestAPIList(this.constructor as any, nextOptions);
46
+ }
47
+
48
+ async *iterPages(): AsyncGenerator<this> {
49
+ let page: this = this;
50
+ yield page;
51
+ while (page.hasNextPage()) {
52
+ page = await page.getNextPage();
53
+ yield page;
54
+ }
55
+ }
56
+
57
+ async *[Symbol.asyncIterator](): AsyncGenerator<Item> {
58
+ for await (const page of this.iterPages()) {
59
+ for (const item of page.getPaginatedItems()) {
60
+ yield item;
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ /**
67
+ * This subclass of Promise will resolve to an instantiated Page once the request completes.
68
+ *
69
+ * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
70
+ *
71
+ * for await (const item of client.items.list()) {
72
+ * console.log(item)
73
+ * }
74
+ */
75
+ export class PagePromise<
76
+ PageClass extends AbstractPage<Item>,
77
+ Item = ReturnType<PageClass['getPaginatedItems']>[number],
78
+ >
79
+ extends APIPromise<PageClass>
80
+ implements AsyncIterable<Item>
81
+ {
82
+ constructor(
83
+ client: CheckoutIntents,
84
+ request: Promise<APIResponseProps>,
85
+ Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass,
86
+ ) {
87
+ super(
88
+ client,
89
+ request,
90
+ async (client, props) =>
91
+ new Page(client, props.response, await defaultParseResponse(client, props), props.options),
92
+ );
93
+ }
94
+
95
+ /**
96
+ * Allow auto-paginating iteration on an unawaited list call, eg:
97
+ *
98
+ * for await (const item of client.items.list()) {
99
+ * console.log(item)
100
+ * }
101
+ */
102
+ async *[Symbol.asyncIterator](): AsyncGenerator<Item> {
103
+ const page = await this;
104
+ for await (const item of page) {
105
+ yield item;
106
+ }
107
+ }
108
+ }
109
+
110
+ export interface CursorPaginationResponse<Item> {
111
+ data: Array<Item>;
112
+
113
+ pageInfo: CursorPaginationResponse.PageInfo;
114
+ }
115
+
116
+ export namespace CursorPaginationResponse {
117
+ export interface PageInfo {
118
+ endCursor?: string | null;
119
+
120
+ hasNextPage?: boolean;
121
+
122
+ hasPreviousPage?: boolean;
123
+
124
+ startCursor?: string | null;
125
+ }
126
+ }
127
+
128
+ export interface CursorPaginationParams {
129
+ limit?: number;
130
+
131
+ after?: string;
132
+
133
+ before?: string;
134
+ }
135
+
136
+ export class CursorPagination<Item> extends AbstractPage<Item> implements CursorPaginationResponse<Item> {
137
+ data: Array<Item>;
138
+
139
+ pageInfo: CursorPaginationResponse.PageInfo;
140
+
141
+ constructor(
142
+ client: CheckoutIntents,
143
+ response: Response,
144
+ body: CursorPaginationResponse<Item>,
145
+ options: FinalRequestOptions,
146
+ ) {
147
+ super(client, response, body, options);
148
+
149
+ this.data = body.data || [];
150
+ this.pageInfo = body.pageInfo || {};
151
+ }
152
+
153
+ getPaginatedItems(): Item[] {
154
+ return this.data ?? [];
155
+ }
156
+
157
+ nextPageRequestOptions(): PageRequestOptions | null {
158
+ if ((this.options.query as Record<string, unknown>)?.['before']) {
159
+ // in reverse
160
+ const startCursor = this.pageInfo?.startCursor;
161
+ if (!startCursor) {
162
+ return null;
163
+ }
164
+
165
+ return {
166
+ ...this.options,
167
+ query: {
168
+ ...maybeObj(this.options.query),
169
+ before: startCursor,
170
+ },
171
+ };
172
+ }
173
+
174
+ const cursor = this.pageInfo?.endCursor;
175
+ if (!cursor) {
176
+ return null;
177
+ }
178
+
179
+ return {
180
+ ...this.options,
181
+ query: {
182
+ ...maybeObj(this.options.query),
183
+ after: cursor,
184
+ },
185
+ };
186
+ }
187
+ }
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ export { CheckoutIntents as default } from './client';
5
5
  export { type Uploadable, toFile } from './core/uploads';
6
6
  export { APIPromise } from './core/api-promise';
7
7
  export { CheckoutIntents, type ClientOptions } from './client';
8
+ export { PagePromise } from './core/pagination';
8
9
  export {
9
10
  CheckoutIntentsError,
10
11
  APIError,
@@ -0,0 +1,13 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2014, Nathan LaFreniere and other [contributors](https://github.com/puruvj/neoqs/graphs/contributors) All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+
9
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
+
11
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,3 @@
1
+ # qs
2
+
3
+ This is a vendored version of [neoqs](https://github.com/PuruVJ/neoqs) which is a TypeScript rewrite of [qs](https://github.com/ljharb/qs), a query string library.
@@ -0,0 +1,10 @@
1
+ import type { Format } from './types';
2
+
3
+ export const default_format: Format = 'RFC3986';
4
+ export const default_formatter = (v: PropertyKey) => String(v);
5
+ export const formatters: Record<Format, (str: PropertyKey) => string> = {
6
+ RFC1738: (v: PropertyKey) => String(v).replace(/%20/g, '+'),
7
+ RFC3986: default_formatter,
8
+ };
9
+ export const RFC1738 = 'RFC1738';
10
+ export const RFC3986 = 'RFC3986';
@@ -0,0 +1,13 @@
1
+ import { default_format, formatters, RFC1738, RFC3986 } from './formats';
2
+
3
+ const formats = {
4
+ formatters,
5
+ RFC1738,
6
+ RFC3986,
7
+ default: default_format,
8
+ };
9
+
10
+ export { stringify } from './stringify';
11
+ export { formats };
12
+
13
+ export type { DefaultDecoder, DefaultEncoder, Format, ParseOptions, StringifyOptions } from './types';