Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit b0ed54a

Browse files
authored
Merge pull request #1262 from Shopify/fix_customer_search_pagination
Allow paginating customer / gift card searches
2 parents f66db62 + 8d885bd commit b0ed54a

File tree

16 files changed

+62
-28
lines changed

16 files changed

+62
-28
lines changed

.changeset/gentle-doors-complain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@shopify/shopify-api": patch
3+
---
4+
5+
Enabled returning the full response object in `Customer.search()` and `GiftCard.search()`, so that apps can paginate through the results.

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ rollup.config.cjs
33
.eslintrc.cjs
44
node_modules/
55
dist/
6+
packages/shopify-api/rest/admin

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages/shopify-api/rest/admin

packages/shopify-api/docs/guides/rest-resources.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ To make it easier to interact with the API, this library provides resource class
1010
1111
## Resource Methods
1212

13-
| Resource Method | Description | Admin API endpoint | Return |
14-
| --------------- | ----------- | ------------------ | ------ |
15-
| `find` | Fetch a single resource by its ID | `GET /admin/api/{version}/{resource_name}/{resource_id}.json` | A single resource |
16-
| `all` | Fetch all resources of a given type | `GET /admin/api/{version}/{resource_name}.json` | [An array of resources](#all-return-value) |
17-
| `count` | Fetch the number of resources of a given type | `GET /admin/api/{version}/{resource_name}/count.json` | Integer |
18-
| `save` | If the primary key for the resource **is not set**, a new resource will be created in Shopify | `POST /admin/api/{version}/{resource_name}.json` | void Promise (If option update: true is passed, the resource will be updated with the returned data.) |
19-
| `save` | If the primary key for a resource **is set** the resource in Shopify will be updated | `PUT /admin/api/{version}/{resource_name}/{resource_id}.json` | Promise void (If option update: true is passed, the resource will be updated with the returned data.) |
20-
| `delete` | Delete an existing resource | `DELETE /admin/api/{version}/{resource_name}/{resource_id}.json` | void Promise |
13+
| Resource Method | Description | Admin API endpoint | Return |
14+
| --------------- | --------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
15+
| `find` | Fetch a single resource by its ID | `GET /admin/api/{version}/{resource_name}/{resource_id}.json` | A single resource |
16+
| `all` | Fetch all resources of a given type | `GET /admin/api/{version}/{resource_name}.json` | [An array of resources](#all-return-value) |
17+
| `count` | Fetch the number of resources of a given type | `GET /admin/api/{version}/{resource_name}/count.json` | Integer |
18+
| `save` | If the primary key for the resource **is not set**, a new resource will be created in Shopify | `POST /admin/api/{version}/{resource_name}.json` | void Promise (If option update: true is passed, the resource will be updated with the returned data.) |
19+
| `save` | If the primary key for a resource **is set** the resource in Shopify will be updated | `PUT /admin/api/{version}/{resource_name}/{resource_id}.json` | Promise void (If option update: true is passed, the resource will be updated with the returned data.) |
20+
| `delete` | Delete an existing resource | `DELETE /admin/api/{version}/{resource_name}/{resource_id}.json` | void Promise |
2121

2222
Some resources will have additional methods to help with common interactions, such as the [orders method on the customer resource](https://shopify.dev/docs/api/admin-rest/2023-07/resources/customer#get-customers-customer-id-orders). Review the [REST API reference](https://shopify.dev/docs/api/admin-rest) documentation for more information.
2323

2424
### All Return Value
25+
2526
The all method will return an array of resources, along with the response headers and pagination information.
2627

2728
```
@@ -110,13 +111,12 @@ await product.save({
110111
### Create a resource
111112

112113
```ts
113-
114114
const product = new shopify.rest.Product({session: session});
115-
product.title = "Burton Custom Freestyle 151";
116-
product.body_html = "<strong>Good snowboard!</strong>";
117-
product.vendor = "Burton";
118-
product.product_type = "Snowboard";
119-
product.status = "draft";
115+
product.title = 'Burton Custom Freestyle 151';
116+
product.body_html = '<strong>Good snowboard!</strong>';
117+
product.vendor = 'Burton';
118+
product.product_type = 'Snowboard';
119+
product.status = 'draft';
120120

121121
// After promise resolves, product will be updated with the returned data
122122
await product.save({
@@ -154,7 +154,6 @@ console.log(products.pageInfo);
154154

155155
// The response headers
156156
console.log(products.headers);
157-
158157
```
159158

160159
## Mounting REST resources
@@ -201,4 +200,8 @@ do {
201200
} while (pageInfo?.nextPage);
202201
```
203202

203+
> [!NOTE]
204+
> Some resources have a `search()` method which also supports pagination, in the same way `all()` does.
205+
> To return the full response information from `search()`, set the `returnFullResponse` option when calling it.
206+
204207
[Back to guide index](../../README.md#guides)

packages/shopify-api/rest/admin/2022-10/customer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface SearchArgs {
5151
query?: unknown;
5252
limit?: unknown;
5353
fields?: unknown;
54+
returnFullResponse?: boolean;
5455
}
5556
interface AccountActivationUrlArgs {
5657
[key: string]: unknown;
@@ -193,8 +194,9 @@ export class Customer extends Base {
193194
query = null,
194195
limit = null,
195196
fields = null,
197+
returnFullResponse = false,
196198
...otherArgs
197-
}: SearchArgs
199+
}: SearchArgs,
198200
): Promise<unknown> {
199201
const response = await this.request<Customer>({
200202
http_method: "get",
@@ -206,7 +208,7 @@ export class Customer extends Base {
206208
entity: null,
207209
});
208210

209-
return response ? response.body : null;
211+
return returnFullResponse ? response : response?.body;
210212
}
211213

212214
public async account_activation_url(

packages/shopify-api/rest/admin/2022-10/gift_card.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface SearchArgs {
3535
created_at_max?: unknown;
3636
updated_at_min?: unknown;
3737
updated_at_max?: unknown;
38+
returnFullResponse?: boolean;
3839
}
3940
interface DisableArgs {
4041
[key: string]: unknown;
@@ -126,6 +127,7 @@ export class GiftCard extends Base {
126127
created_at_max = null,
127128
updated_at_min = null,
128129
updated_at_max = null,
130+
returnFullResponse = false,
129131
...otherArgs
130132
}: SearchArgs
131133
): Promise<unknown> {
@@ -139,7 +141,7 @@ export class GiftCard extends Base {
139141
entity: null,
140142
});
141143

142-
return response ? response.body : null;
144+
return returnFullResponse ? response : response?.body;
143145
}
144146

145147
public async disable(

packages/shopify-api/rest/admin/2023-01/customer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface SearchArgs {
5151
query?: unknown;
5252
limit?: unknown;
5353
fields?: unknown;
54+
returnFullResponse?: boolean;
5455
}
5556
interface AccountActivationUrlArgs {
5657
[key: string]: unknown;
@@ -193,6 +194,7 @@ export class Customer extends Base {
193194
query = null,
194195
limit = null,
195196
fields = null,
197+
returnFullResponse = false,
196198
...otherArgs
197199
}: SearchArgs
198200
): Promise<unknown> {
@@ -206,7 +208,7 @@ export class Customer extends Base {
206208
entity: null,
207209
});
208210

209-
return response ? response.body : null;
211+
return returnFullResponse ? response : response?.body;
210212
}
211213

212214
public async account_activation_url(

packages/shopify-api/rest/admin/2023-01/gift_card.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface SearchArgs {
3535
created_at_max?: unknown;
3636
updated_at_min?: unknown;
3737
updated_at_max?: unknown;
38+
returnFullResponse?: boolean;
3839
}
3940
interface DisableArgs {
4041
[key: string]: unknown;
@@ -126,6 +127,7 @@ export class GiftCard extends Base {
126127
created_at_max = null,
127128
updated_at_min = null,
128129
updated_at_max = null,
130+
returnFullResponse = false,
129131
...otherArgs
130132
}: SearchArgs
131133
): Promise<unknown> {
@@ -139,7 +141,7 @@ export class GiftCard extends Base {
139141
entity: null,
140142
});
141143

142-
return response ? response.body : null;
144+
return returnFullResponse ? response : response?.body;
143145
}
144146

145147
public async disable(

packages/shopify-api/rest/admin/2023-04/customer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface SearchArgs {
5151
query?: unknown;
5252
limit?: unknown;
5353
fields?: unknown;
54+
returnFullResponse?: boolean;
5455
}
5556
interface AccountActivationUrlArgs {
5657
[key: string]: unknown;
@@ -193,6 +194,7 @@ export class Customer extends Base {
193194
query = null,
194195
limit = null,
195196
fields = null,
197+
returnFullResponse = false,
196198
...otherArgs
197199
}: SearchArgs
198200
): Promise<unknown> {
@@ -206,7 +208,7 @@ export class Customer extends Base {
206208
entity: null,
207209
});
208210

209-
return response ? response.body : null;
211+
return returnFullResponse ? response : response?.body;
210212
}
211213

212214
public async account_activation_url(

packages/shopify-api/rest/admin/2023-04/gift_card.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface SearchArgs {
3535
created_at_max?: unknown;
3636
updated_at_min?: unknown;
3737
updated_at_max?: unknown;
38+
returnFullResponse?: boolean;
3839
}
3940
interface DisableArgs {
4041
[key: string]: unknown;
@@ -126,6 +127,7 @@ export class GiftCard extends Base {
126127
created_at_max = null,
127128
updated_at_min = null,
128129
updated_at_max = null,
130+
returnFullResponse = false,
129131
...otherArgs
130132
}: SearchArgs
131133
): Promise<unknown> {
@@ -139,7 +141,7 @@ export class GiftCard extends Base {
139141
entity: null,
140142
});
141143

142-
return response ? response.body : null;
144+
return returnFullResponse ? response : response?.body;
143145
}
144146

145147
public async disable(

packages/shopify-api/rest/admin/2023-07/customer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface SearchArgs {
5151
query?: unknown;
5252
limit?: unknown;
5353
fields?: unknown;
54+
returnFullResponse?: boolean;
5455
}
5556
interface AccountActivationUrlArgs {
5657
[key: string]: unknown;
@@ -193,6 +194,7 @@ export class Customer extends Base {
193194
query = null,
194195
limit = null,
195196
fields = null,
197+
returnFullResponse = false,
196198
...otherArgs
197199
}: SearchArgs
198200
): Promise<unknown> {
@@ -206,7 +208,7 @@ export class Customer extends Base {
206208
entity: null,
207209
});
208210

209-
return response ? response.body : null;
211+
return returnFullResponse ? response : response?.body;
210212
}
211213

212214
public async account_activation_url(

packages/shopify-api/rest/admin/2023-07/gift_card.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface SearchArgs {
3535
created_at_max?: unknown;
3636
updated_at_min?: unknown;
3737
updated_at_max?: unknown;
38+
returnFullResponse?: boolean;
3839
}
3940
interface DisableArgs {
4041
[key: string]: unknown;
@@ -126,6 +127,7 @@ export class GiftCard extends Base {
126127
created_at_max = null,
127128
updated_at_min = null,
128129
updated_at_max = null,
130+
returnFullResponse = false,
129131
...otherArgs
130132
}: SearchArgs
131133
): Promise<unknown> {
@@ -139,7 +141,7 @@ export class GiftCard extends Base {
139141
entity: null,
140142
});
141143

142-
return response ? response.body : null;
144+
return returnFullResponse ? response : response?.body;
143145
}
144146

145147
public async disable(

packages/shopify-api/rest/admin/2023-10/customer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface SearchArgs {
5151
query?: unknown;
5252
limit?: unknown;
5353
fields?: unknown;
54+
returnFullResponse?: boolean;
5455
}
5556
interface AccountActivationUrlArgs {
5657
[key: string]: unknown;
@@ -193,6 +194,7 @@ export class Customer extends Base {
193194
query = null,
194195
limit = null,
195196
fields = null,
197+
returnFullResponse = false,
196198
...otherArgs
197199
}: SearchArgs
198200
): Promise<unknown> {
@@ -206,7 +208,7 @@ export class Customer extends Base {
206208
entity: null,
207209
});
208210

209-
return response ? response.body : null;
211+
return returnFullResponse ? response : response?.body;
210212
}
211213

212214
public async account_activation_url(

packages/shopify-api/rest/admin/2023-10/gift_card.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface SearchArgs {
3535
created_at_max?: unknown;
3636
updated_at_min?: unknown;
3737
updated_at_max?: unknown;
38+
returnFullResponse?: boolean;
3839
}
3940
interface DisableArgs {
4041
[key: string]: unknown;
@@ -126,6 +127,7 @@ export class GiftCard extends Base {
126127
created_at_max = null,
127128
updated_at_min = null,
128129
updated_at_max = null,
130+
returnFullResponse = false,
129131
...otherArgs
130132
}: SearchArgs
131133
): Promise<unknown> {
@@ -139,7 +141,7 @@ export class GiftCard extends Base {
139141
entity: null,
140142
});
141143

142-
return response ? response.body : null;
144+
return returnFullResponse ? response : response?.body;
143145
}
144146

145147
public async disable(

packages/shopify-api/rest/admin/2024-01/customer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface SearchArgs {
5151
query?: unknown;
5252
limit?: unknown;
5353
fields?: unknown;
54+
returnFullResponse?: boolean;
5455
}
5556
interface AccountActivationUrlArgs {
5657
[key: string]: unknown;
@@ -193,6 +194,7 @@ export class Customer extends Base {
193194
query = null,
194195
limit = null,
195196
fields = null,
197+
returnFullResponse = false,
196198
...otherArgs
197199
}: SearchArgs
198200
): Promise<unknown> {
@@ -206,7 +208,7 @@ export class Customer extends Base {
206208
entity: null,
207209
});
208210

209-
return response ? response.body : null;
211+
return returnFullResponse ? response : response?.body;
210212
}
211213

212214
public async account_activation_url(

packages/shopify-api/rest/admin/2024-01/gift_card.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface SearchArgs {
3535
created_at_max?: unknown;
3636
updated_at_min?: unknown;
3737
updated_at_max?: unknown;
38+
returnFullResponse?: boolean;
3839
}
3940
interface DisableArgs {
4041
[key: string]: unknown;
@@ -126,6 +127,7 @@ export class GiftCard extends Base {
126127
created_at_max = null,
127128
updated_at_min = null,
128129
updated_at_max = null,
130+
returnFullResponse = false,
129131
...otherArgs
130132
}: SearchArgs
131133
): Promise<unknown> {
@@ -139,7 +141,7 @@ export class GiftCard extends Base {
139141
entity: null,
140142
});
141143

142-
return response ? response.body : null;
144+
return returnFullResponse ? response : response?.body;
143145
}
144146

145147
public async disable(

0 commit comments

Comments
 (0)