-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
223 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ module.exports = { | |
__HOST__: true, | ||
__PATH__: true, | ||
__COMPONENTS__: true, | ||
$Shape: true, | ||
}, | ||
|
||
rules: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* @flow */ | ||
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src"; | ||
import { request } from "@krakenjs/belter/src"; | ||
import { getSessionID, getPartnerAttributionID } from "@paypal/sdk-client/src"; | ||
|
||
import { callRestAPI } from "../lib"; | ||
import { HEADERS } from "../constants/api"; | ||
|
||
type HTTPRequestOptions = {| | ||
// eslint-disable-next-line flowtype/no-weak-types | ||
data: any, | ||
baseURL?: string, | ||
accessToken?: string, | ||
method?: string, // TODO do we have an available type for this in Flow? | ||
|}; | ||
|
||
interface HTTPClientType { | ||
accessToken: ?string; | ||
baseURL: ?string; | ||
} | ||
|
||
type HTTPClientOptions = {| | ||
accessToken: ?string, | ||
baseURL: ?string, | ||
|}; | ||
|
||
class HTTPClient implements HTTPClientType { | ||
accessToken: ?string; | ||
baseURL: ?string; | ||
|
||
constructor(options?: $Shape<HTTPClientOptions> = {}) { | ||
this.accessToken = options.accessToken; | ||
this.baseURL = options.baseURL; | ||
} | ||
|
||
setAccessToken(token: string) { | ||
this.accessToken = token; | ||
} | ||
} | ||
|
||
export class RestClient extends HTTPClient { | ||
request({ baseURL, ...rest }: HTTPRequestOptions): ZalgoPromise<{ ... }> { | ||
return callRestAPI({ | ||
url: baseURL ?? this.baseURL ?? "", | ||
accessToken: this.accessToken, | ||
...rest, | ||
}); | ||
} | ||
} | ||
|
||
const GRAPHQL_URI = "/graphql"; | ||
|
||
type GQLQuery = {| | ||
query: string, | ||
variables: { ... }, | ||
|}; | ||
|
||
export function callGraphQLAPI({ | ||
accessToken, | ||
baseURL, | ||
data: query, | ||
}: {| | ||
accessToken: ?string, | ||
baseURL: string, | ||
data: GQLQuery, | ||
// eslint-disable-next-line flowtype/no-weak-types | ||
|}): ZalgoPromise<any> { | ||
if (!accessToken) { | ||
throw new Error( | ||
`No access token passed to GraphQL request ${baseURL}${GRAPHQL_URI}` | ||
); | ||
} | ||
|
||
const requestHeaders = { | ||
[HEADERS.AUTHORIZATION]: `Bearer ${accessToken}`, | ||
[HEADERS.CONTENT_TYPE]: "application/json", | ||
[HEADERS.PARTNER_ATTRIBUTION_ID]: getPartnerAttributionID() ?? "", | ||
[HEADERS.CLIENT_METADATA_ID]: getSessionID(), | ||
}; | ||
|
||
return request({ | ||
method: "post", | ||
url: `${baseURL}${GRAPHQL_URI}`, | ||
headers: requestHeaders, | ||
json: query, | ||
}).then(({ status, body }) => { | ||
// TODO handle body.errors | ||
if (status !== 200) { | ||
throw new Error(`${baseURL}${GRAPHQL_URI} returned status ${status}`); | ||
} | ||
|
||
return body.data; | ||
}); | ||
} | ||
|
||
export class GraphQLClient extends HTTPClient { | ||
request({ | ||
baseURL, | ||
data, | ||
accessToken, | ||
}: // eslint-disable-next-line flowtype/no-weak-types | ||
$Shape<HTTPRequestOptions> & {| data: GQLQuery |} & any): ZalgoPromise<any> { | ||
return callGraphQLAPI({ | ||
accessToken: accessToken ?? this.accessToken, | ||
data, | ||
baseURL: baseURL ?? this.baseURL ?? "", | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters