@shopify/shopify-api
Version:
Shopify API Library for Node - accelerate development with support for authentication, graphql proxy, webhooks
44 lines (40 loc) • 1.6 kB
JavaScript
;
var common = require('../../clients/common.js');
var types = require('../../clients/types.js');
var fetchRequest = require('../../utils/fetch-request.js');
var shopValidator = require('../../utils/shop-validator.js');
var createSession = require('./create-session.js');
const RefreshTokenGrantType = 'refresh_token';
function refreshToken(config) {
return async ({ shop, refreshToken }) => {
const body = {
client_id: config.apiKey,
client_secret: config.apiSecretKey,
refresh_token: refreshToken,
grant_type: RefreshTokenGrantType,
};
const cleanShop = shopValidator.sanitizeShop(config)(shop, true);
const postResponse = await fetchRequest.fetchRequestFactory(config)(`https://${cleanShop}/admin/oauth/access_token`, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': types.DataType.JSON,
Accept: types.DataType.JSON,
},
});
if (!postResponse.ok) {
common.throwFailedRequest(await postResponse.json(), false, postResponse);
}
return {
session: createSession.createSession({
accessTokenResponse: await postResponse.json(),
shop: cleanShop,
// We need to keep this as an empty string as our template DB schemas have this required
state: '',
config,
}),
};
};
}
exports.refreshToken = refreshToken;
//# sourceMappingURL=refresh-token.js.map