Automatically retry partners requests that fail with 401
WHY are these changes introduced?
To improve error handling in the Partners API client by adding support for token refresh when unauthorized responses are received.
WHAT is this pull request doing?
Adds an unauthorized handler to the Partners API client that automatically refreshes the token when an unauthorized response is received. This prevents API requests from failing due to expired tokens by:
- Adding an optional
unauthorizedHandlerparameter topartnersRequestandpartnersRequestDocfunctions - Creating a handler in the
PartnersClientclass that refreshes the token when needed - Updating tests to expect the new handler parameter
Measuring impact
We should see an improvement in our internal command success metrics.
Look out for
Do we need to increase the scope of this? Other services and such.
Checklist
- [x] I've considered possible cross-platform impacts (Mac, Linux, Windows)
- [x] I've considered possible documentation changes
-
#5689
-
#5688
-
#5687
-
#5646
π (View in Graphite)
-
main
This stack of pull requests is managed by Graphite. Learn more about stacking.
Coverage report
St.:grey_question: |
Category | Percentage | Covered / Total |
|---|---|---|---|
| π‘ | Statements | 76.59% (-0.11% π») |
9513/12420 |
| π‘ | Branches | 71.75% (-0.06% π») |
4679/6521 |
| π‘ | Functions | 76.41% (-0.06% π») |
2465/3226 |
| π‘ | Lines | 77.12% (-0.11% π») |
8994/11662 |
Show files with reduced coverage π»
St.:grey_question: |
File | Statements | Branches | Functions | Lines |
|---|---|---|---|---|---|
| π΄ | ... / partners-client.ts |
25% (-0.66% π») |
28.57% (-3.01% π») |
18.03% (+1.08% πΌ) |
24.68% (-0.66% π») |
| π’ | ... / api.ts |
83.49% (-0.17% π») |
71.01% (+0.25% πΌ) |
92.31% (-7.69% π») |
83.65% (+0.65% πΌ) |
| π‘ | ... / graphql.ts |
80% (-20% π») |
60% (-11.43% π») |
90.91% (-9.09% π») |
79.59% (-20.41% π») |
Test suite run success
2217 tests passing in 966 suites.
Report generated by π§ͺjest coverage report action from 23f1745887e8157507e578f8935e4385c5dcd51d
We detected some changes at packages/*/src and there are no updates in the .changeset. If the changes are user-facing, run "pnpm changeset add" to track your changes and include them in the next release CHANGELOG.
Differences in type declarations
We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:
- Some seemingly private modules might be re-exported through public modules.
- If the branch is behind
mainyou might see odd diffs, rebasemaininto this branch.
New type declarations
We found no new type declarations in this PR
Existing type declarations
packages/cli-kit/dist/private/node/api.d.ts
@@ -11,6 +11,13 @@ type RequestOptions<T> = {
request: () => Promise<T>;
url: string;
} & NetworkRetryBehaviour;
+export interface UnauthorizedHandlerThrow {
+ action: 'throw';
+}
+type UnauthorizedHandlerContinue = {
+ action: 'continue';
+} | undefined;
+export type UnauthorizedHandlerResult = Promise<UnauthorizedHandlerThrow | UnauthorizedHandlerContinue>;
export declare function simpleRequestWithDebugLog<T extends {
headers: Headers;
status: number;
@@ -35,7 +42,7 @@ export declare function simpleRequestWithDebugLog<T extends {
export declare function retryAwareRequest<T extends {
headers: Headers;
status: number;
-}>(requestOptions: RequestOptions<T>, errorHandler?: (error: unknown, requestId: string | undefined) => unknown, unauthorizedHandler?: () => Promise<void>, retryOptions?: {
+}>(requestOptions: RequestOptions<T>, errorHandler?: (error: unknown, requestId: string | undefined) => unknown, unauthorizedHandler?: () => UnauthorizedHandlerResult, retryOptions?: {
limitRetriesTo?: number;
defaultDelayMs?: number;
scheduleDelay: (fn: () => void, delay: number) => void;
packages/cli-kit/dist/public/node/api/graphql.d.ts
@@ -1,3 +1,4 @@
+import { UnauthorizedHandlerResult, UnauthorizedHandlerThrow } from '../../../private/node/api.js';
import { ConfSchema, TimeInterval } from '../../../private/node/conf-store.js';
import { LocalStorage } from '../local-storage.js';
import { rawRequest, RequestDocument, Variables } from 'graphql-request';
@@ -16,6 +17,11 @@ export interface CacheOptions {
cacheExtraKey?: string;
cacheStore?: LocalStorage<ConfSchema>;
}
+interface RefreshTokenOnAuthorizedResponseRetryWithNewToken {
+ action: 'retry';
+ token: string;
+}
+export type RefreshTokenOnAuthorizedResponse = Promise<UnauthorizedHandlerThrow | RefreshTokenOnAuthorizedResponseRetryWithNewToken>;
interface GraphQLRequestBaseOptions<TResult> {
api: string;
url: string;
@@ -25,18 +31,19 @@ interface GraphQLRequestBaseOptions<TResult> {
};
responseOptions?: GraphQLResponseOptions<TResult>;
cacheOptions?: CacheOptions;
+ refreshTokenOnAuthorizedResponse?: () => RefreshTokenOnAuthorizedResponse;
}
export type GraphQLRequestOptions<T> = GraphQLRequestBaseOptions<T> & {
query: RequestDocument;
variables?: Variables;
- unauthorizedHandler?: () => Promise<void>;
+ unauthorizedHandler?: () => UnauthorizedHandlerResult;
};
export type GraphQLRequestDocOptions<TResult, TVariables> = GraphQLRequestBaseOptions<TResult> & {
query: TypedDocumentNode<TResult, TVariables> | TypedDocumentNode<TResult, Exact<{
[key: string]: never;
}>>;
variables?: TVariables;
- unauthorizedHandler?: () => Promise<void>;
+ unauthorizedHandler?: () => UnauthorizedHandlerResult;
};
export interface GraphQLResponseOptions<T> {
handleErrors?: boolean;
packages/cli-kit/dist/public/node/api/partners.d.ts
@@ -1,4 +1,4 @@
-import { GraphQLVariables, GraphQLResponse, CacheOptions } from './graphql.js';
+import { GraphQLVariables, GraphQLResponse, CacheOptions, RefreshTokenOnAuthorizedResponse } from './graphql.js';
import { Variables } from 'graphql-request';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
/**
@@ -8,9 +8,10 @@ import { TypedDocumentNode } from '@graphql-typed-document-node/core';
* @param token - Partners token.
* @param variables - GraphQL variables to pass to the query.
* @param cacheOptions - Cache options.
+ * @param refreshTokenOnAuthorizedResponse - Optional handler for unauthorized requests.
* @returns The response of the query of generic type <T>.
*/
-export declare function partnersRequest<T>(query: string, token: string, variables?: GraphQLVariables, cacheOptions?: CacheOptions): Promise<T>;
+export declare function partnersRequest<T>(query: string, token: string, variables?: GraphQLVariables, cacheOptions?: CacheOptions, refreshTokenOnAuthorizedResponse?: () => RefreshTokenOnAuthorizedResponse): Promise<T>;
export declare const generateFetchAppLogUrl: (cursor?: string, filters?: {
status?: string;
source?: string;
@@ -21,9 +22,10 @@ export declare const generateFetchAppLogUrl: (cursor?: string, filters?: {
* @param query - GraphQL query to execute.
* @param token - Partners token.
* @param variables - GraphQL variables to pass to the query.
+ * @param refreshTokenOnAuthorizedResponse - Optional handler for unauthorized requests.
* @returns The response of the query of generic type <TResult>.
*/
-export declare function partnersRequestDoc<TResult, TVariables extends Variables>(query: TypedDocumentNode<TResult, TVariables>, token: string, variables?: TVariables): Promise<TResult>;
+export declare function partnersRequestDoc<TResult, TVariables extends Variables>(query: TypedDocumentNode<TResult, TVariables>, token: string, variables?: TVariables, refreshTokenOnAuthorizedResponse?: () => RefreshTokenOnAuthorizedResponse): Promise<TResult>;
/**
* Sets the next deprecation date from [GraphQL response extensions](https://www.apollographql.com/docs/resources/graphql-glossary/#extensions)
* if objects contain a (ISO 8601-formatted string).
This PR seems inactive. If it's still relevant, please add a comment saying so. Otherwise, take no action. β If there's no activity within a week, then a bot will automatically close this. Thanks for helping to improve Shopify's dev tooling and experience.