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

Commit 0fca891

Browse files
committed
Don't ignore previous headers when beginning OAuth
1 parent 16730c5 commit 0fca891

File tree

7 files changed

+44
-8
lines changed

7 files changed

+44
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
99

10+
- [Patch] Don't ignore previous headers when beginning OAuth [#652](https://github.com/Shopify/shopify-api-js/pull/652)
1011
- [Patch] Export missing client types from package [#648](https://github.com/Shopify/shopify-api-js/pull/648)
1112
- [Patch] Add an info-level log of API library version and runtime environment string during initialization, to aid in troubleshooting [650](https://github.com/Shopify/shopify-api-js/pull/650)
1213

adapters/node/adapter.ts

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ export async function nodeConvertRequest(
2929
};
3030
}
3131

32+
export async function nodeConvertIncomingResponse(
33+
adapterArgs: NodeAdapterArgs,
34+
): Promise<NormalizedResponse> {
35+
return {
36+
statusCode: adapterArgs.rawResponse.statusCode,
37+
statusText: adapterArgs.rawResponse.statusMessage,
38+
headers: canonicalizeHeaders(
39+
adapterArgs.rawResponse.getHeaders() as any as Headers,
40+
),
41+
} as NormalizedResponse;
42+
}
43+
3244
export async function nodeConvertAndSendResponse(
3345
response: NormalizedResponse,
3446
adapterArgs: NodeAdapterArgs,

adapters/node/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import crypto from 'crypto';
33
import {
44
setAbstractFetchFunc,
55
setAbstractConvertRequestFunc,
6+
setAbstractConvertIncomingResponseFunc,
67
setAbstractConvertResponseFunc,
78
setAbstractConvertHeadersFunc,
89
setAbstractRuntimeString,
@@ -12,13 +13,15 @@ import {
1213
import {
1314
nodeFetch,
1415
nodeConvertRequest,
16+
nodeConvertIncomingResponse,
1517
nodeConvertAndSendResponse,
1618
nodeConvertAndSetHeaders,
1719
nodeRuntimeString,
1820
} from './adapter';
1921

2022
setAbstractFetchFunc(nodeFetch);
2123
setAbstractConvertRequestFunc(nodeConvertRequest);
24+
setAbstractConvertIncomingResponseFunc(nodeConvertIncomingResponse);
2225
setAbstractConvertResponseFunc(nodeConvertAndSendResponse);
2326
setAbstractConvertHeadersFunc(nodeConvertAndSetHeaders);
2427
setAbstractRuntimeString(nodeRuntimeString);

docs/guides/runtimes.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ These are the functions you'll need to create:
1818
- `setAbstractRuntimeString`
1919
- `setCrypto`
2020

21+
For runtimes that pass in a response object, such as Node.js, you'll also need to create:
22+
23+
- `setAbstractConvertIncomingResponseFunc`
24+
2125
Below is a _very_ simplified example with some functions:
2226

2327
```ts

lib/auth/oauth/oauth.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {httpClientClass} from '../../clients/http_client/http_client';
1111
import {DataType, RequestReturn} from '../../clients/http_client/types';
1212
import {
1313
abstractConvertRequest,
14+
abstractConvertIncomingResponse,
1415
abstractConvertResponse,
1516
abstractConvertHeaders,
1617
AdapterResponse,
@@ -55,8 +56,9 @@ export function begin(config: ConfigInterface) {
5556

5657
const cleanShop = sanitizeShop(config)(shop, true)!;
5758
const request = await abstractConvertRequest(adapterArgs);
59+
const response = await abstractConvertIncomingResponse(adapterArgs);
5860

59-
const cookies = new Cookies(request, {} as NormalizedResponse, {
61+
const cookies = new Cookies(request, response, {
6062
keys: [config.apiSecretKey],
6163
secure: true,
6264
});
@@ -81,13 +83,12 @@ export function begin(config: ConfigInterface) {
8183
processedQuery.putAll(query);
8284

8385
const redirectUrl = `https://${cleanShop}/admin/oauth/authorize${processedQuery.stringify()}`;
84-
const response: NormalizedResponse = {
85-
statusCode: 302,
86-
statusText: 'Found',
87-
headers: {
88-
...cookies.response.headers!,
89-
Location: redirectUrl,
90-
},
86+
response.statusCode = 302;
87+
response.statusText = 'Found';
88+
response.headers = {
89+
...response.headers,
90+
...cookies.response.headers!,
91+
Location: redirectUrl,
9192
};
9293

9394
log.debug(`OAuth started, redirecting to ${redirectUrl}`, {shop, isOnline});

runtime/http/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
AbstractFetchFunc,
33
AbstractConvertRequestFunc,
4+
AbstractConvertIncomingResponseFunc,
45
AbstractConvertResponseFunc,
56
NormalizedResponse,
67
AbstractConvertHeadersFunc,
@@ -41,6 +42,16 @@ export function setAbstractConvertRequestFunc(
4142
abstractConvertRequest = func;
4243
}
4344

45+
// By default we just return an empty NormalizedResponse because not all adapters will need to convert an incoming response
46+
// eslint-disable-next-line import/no-mutable-exports
47+
export let abstractConvertIncomingResponse: AbstractConvertIncomingResponseFunc =
48+
() => Promise.resolve({} as NormalizedResponse);
49+
export function setAbstractConvertIncomingResponseFunc(
50+
func: AbstractConvertIncomingResponseFunc,
51+
) {
52+
abstractConvertIncomingResponse = func;
53+
}
54+
4455
// eslint-disable-next-line import/no-mutable-exports
4556
export let abstractConvertResponse: AbstractConvertResponseFunc = () => {
4657
throw new Error(

runtime/http/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export type AbstractConvertRequestFunc = (
3232
adapterArgs: AdapterArgs,
3333
) => Promise<NormalizedRequest>;
3434

35+
export type AbstractConvertIncomingResponseFunc = (
36+
adapterArgs: AdapterArgs,
37+
) => Promise<NormalizedResponse>;
38+
3539
export type AbstractConvertResponseFunc = (
3640
response: NormalizedResponse,
3741
adapterArgs: AdapterArgs,

0 commit comments

Comments
 (0)