Skip to content

Commit

Permalink
Add entry and api back
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethmv committed Feb 13, 2024
1 parent 70af62e commit eecce52
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions __sdk__.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ module.exports = {
fundingSources: SMART_FUNDING_SOURCES,
cards: SMART_CARDS,
},
"shopper-insights": {
entry: "./src/api/shopper-insights/interface",
},
wallet: {
entry: "./src/interface/wallet",
globals,
Expand Down
69 changes: 69 additions & 0 deletions src/api/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* @flow */

import { getPartnerAttributionID, getSessionID } from "@paypal/sdk-client/src";
import { inlineMemoize, request } from "@krakenjs/belter/src";
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";

import { HEADERS } from "../constants/api";

type RestAPIParams = {|
method?: string,
url: string,
data: Object,
accessToken: ?string,
|};

// TODO: for the fingerprint work we should swap this out for fetch support
// we did not get the time for doing this during the initial project
function callRestAPI({
accessToken,
method,
url,
data,
}: RestAPIParams): ZalgoPromise<Object> {
const partnerAttributionID = getPartnerAttributionID() || "";

if (!accessToken) {
throw new Error(`No access token passed to API request ${url}`);
}

const requestHeaders = {
[HEADERS.AUTHORIZATION]: `Bearer ${accessToken}`,
[HEADERS.CONTENT_TYPE]: `application/json`,
[HEADERS.PARTNER_ATTRIBUTION_ID]: partnerAttributionID,
[HEADERS.CLIENT_METADATA_ID]: getSessionID(),
};

return request({
method,
url,
headers: requestHeaders,
json: data,
}).then(({ status, body, headers: responseHeaders }) => {
if (status >= 300) {
const error = new Error(
`${url} returned status ${status}\n\n${JSON.stringify(body)}`
);

// $FlowFixMe
error.response = { status, headers: responseHeaders, body };

throw error;
}

return body;
});
}

export function callMemoizedRestAPI({
accessToken,
method,
url,
data,
}: RestAPIParams): ZalgoPromise<Object> {
return inlineMemoize(
callMemoizedRestAPI,
() => callRestAPI({ accessToken, method, url, data }),
[accessToken, method, url, JSON.stringify(data)]
);
}

0 comments on commit eecce52

Please sign in to comment.