@atoms-studio/commercelayer-sdk
Advanced tools
Comparing version 0.1.0-alpha.7 to 0.1.0-alpha.8
@@ -0,1 +1,8 @@ | ||
## [0.1.0-alpha.8](https://github.com/atoms-studio/commercelayer-sdk/compare/0.1.0-alpha.7...0.1.0-alpha.8) (2021-07-23) | ||
### Features | ||
* **auth:** add integration support ([e3d5509](https://github.com/atoms-studio/commercelayer-sdk/commit/e3d5509d44d0e38ba3e33b2cf6c7f6859134fd1d)) | ||
## [0.1.0-alpha.7](https://github.com/atoms-studio/commercelayer-sdk/compare/0.1.0-alpha.6...0.1.0-alpha.7) (2021-07-20) | ||
@@ -2,0 +9,0 @@ |
@@ -1,6 +0,3 @@ | ||
export interface GuestResponse { | ||
token: string; | ||
expires: number; | ||
} | ||
export declare const loginAsGuest: () => Promise<GuestResponse>; | ||
import { TokenResponse } from './session'; | ||
export declare const loginAsGuest: () => Promise<TokenResponse>; | ||
//# sourceMappingURL=guest.d.ts.map |
@@ -5,8 +5,9 @@ export declare const Auth: { | ||
getScope: () => string; | ||
loginAsGuest: () => Promise<import("./guest").GuestResponse>; | ||
loginAsGuest: () => Promise<import("./session").TokenResponse>; | ||
loginAsCustomer: (username: string, password: string) => Promise<import("./customer").CustomerData>; | ||
isCustomerLoggedIn: () => boolean; | ||
logoutCustomer: () => void; | ||
getToken: () => import("./cache").TokenCacheEntry; | ||
getCustomerToken: () => import("./cache").TokenCacheEntry; | ||
loginAsIntegration: () => Promise<import("./session").TokenResponse>; | ||
getToken: () => import("./session").TokenCacheEntry; | ||
getCustomerToken: () => import("./session").TokenCacheEntry; | ||
useCustomerSession: (accessToken: string, refreshToken: string, scope: string) => Promise<import("./customer").CustomerData>; | ||
@@ -13,0 +14,0 @@ loadProfile: () => Promise<void>; |
@@ -1,2 +0,9 @@ | ||
import { TokenCacheEntry } from './cache'; | ||
export interface TokenCacheEntry { | ||
token: string; | ||
expires: number; | ||
} | ||
export interface TokenResponse { | ||
token: string; | ||
expires: number; | ||
} | ||
export interface SessionData { | ||
@@ -9,2 +16,10 @@ id: string; | ||
} | ||
export declare type TokenType = 'sales_channel' | 'integration'; | ||
export declare const setTokenType: (type: TokenType) => void; | ||
export declare const getTokenType: () => TokenType; | ||
export declare const tokenCache: Map<string, TokenCacheEntry>; | ||
export declare const INTEGRATION_PREFIX = "integration__"; | ||
export declare const cacheKey: () => string; | ||
export declare const cacheToken: (token: string, expires: number) => void; | ||
export declare const getToken: () => TokenCacheEntry; | ||
export declare const currentCustomerData: SessionData; | ||
@@ -17,2 +32,3 @@ export declare const resetSession: () => void; | ||
export declare const getScope: () => string; | ||
export declare const getCurrentToken: () => TokenCacheEntry; | ||
//# sourceMappingURL=session.d.ts.map |
@@ -33,2 +33,3 @@ 'use strict'; | ||
clientId: '', | ||
clientSecret: '', | ||
refreshTokens: true, | ||
@@ -45,2 +46,3 @@ refreshTokensAttempts: 5, | ||
clientId: defaultConfig.clientId, | ||
clientSecret: defaultConfig.clientSecret, | ||
refreshTokens: defaultConfig.refreshTokens, | ||
@@ -57,3 +59,4 @@ refreshTokensAttempts: defaultConfig.refreshTokensAttempts, | ||
config.host = providedConfig.host; | ||
config.clientId = providedConfig.clientId; | ||
config.clientId = providedConfig.clientId || ''; | ||
config.clientSecret = providedConfig.clientSecret || ''; | ||
config.refreshTokens = !!providedConfig.refreshTokens; | ||
@@ -95,5 +98,2 @@ config.refreshTokensAttempts = Number(providedConfig.refreshTokensAttempts); | ||
} | ||
if (!config.clientId) { | ||
throw new Error('Missing client id'); | ||
} | ||
if (isNaN(config.refreshTokensAttempts)) { | ||
@@ -131,2 +131,33 @@ config.refreshTokensAttempts = defaultConfig.refreshTokensAttempts; | ||
let tokenType = 'sales_channel'; | ||
const setTokenType = (type) => { | ||
tokenType = type; | ||
}; | ||
const getTokenType = () => tokenType; | ||
const tokenCache = new Map(); | ||
const INTEGRATION_PREFIX = 'integration__'; | ||
const cacheKey = () => { | ||
let prefix = ''; | ||
if (tokenType === 'integration') { | ||
prefix = INTEGRATION_PREFIX; | ||
} | ||
return prefix + getCurrentMarkets().join(','); | ||
}; | ||
const cacheToken = (token, expires) => { | ||
tokenCache.set(cacheKey(), { | ||
token, | ||
expires, | ||
}); | ||
}; | ||
const getToken = () => { | ||
const key = cacheKey(); | ||
const cachedValue = tokenCache.get(key); | ||
if (!cachedValue || !cachedValue.token || cachedValue.expires <= Date.now()) { | ||
return { | ||
token: '', | ||
expires: 0, | ||
}; | ||
} | ||
return cachedValue; | ||
}; | ||
const currentCustomerData = { | ||
@@ -171,22 +202,5 @@ id: '', | ||
.join(' '); | ||
const tokenCache = new Map(); | ||
const cacheKey = () => getCurrentMarkets().join(','); | ||
const cacheToken = (token, expires) => { | ||
tokenCache.set(cacheKey(), { | ||
token, | ||
expires, | ||
}); | ||
const getCurrentToken = () => { | ||
return isCustomerLoggedIn() ? getCustomerToken() : getToken(); | ||
}; | ||
const getToken = () => { | ||
const key = cacheKey(); | ||
const cachedValue = tokenCache.get(key); | ||
if (!cachedValue || !cachedValue.token || cachedValue.expires <= Date.now()) { | ||
return { | ||
token: '', | ||
expires: 0, | ||
}; | ||
} | ||
return cachedValue; | ||
}; | ||
@@ -198,2 +212,5 @@ const loginAsGuest = () => __awaiter(void 0, void 0, void 0, function* () { | ||
} | ||
if (!config.clientId) { | ||
throw new Error('Missing client id'); | ||
} | ||
const scope = getScope(); | ||
@@ -203,2 +220,3 @@ if (!scope) { | ||
} | ||
setTokenType('sales_channel'); | ||
// Check if a token is already available | ||
@@ -224,2 +242,35 @@ const cachedValue = getToken(); | ||
const loginAsIntegration = () => __awaiter(void 0, void 0, void 0, function* () { | ||
const config = getConfig(); | ||
if (!config.host) { | ||
throw new Error('You must call "init" before using any Auth method'); | ||
} | ||
if (!config.clientId) { | ||
throw new Error('Missing client id'); | ||
} | ||
if (!config.clientSecret) { | ||
throw new Error('Missing client secret'); | ||
} | ||
setTokenType('integration'); | ||
// Check if a token is already available | ||
const cachedValue = getToken(); | ||
if (cachedValue.token) { | ||
return cachedValue; | ||
} | ||
const scope = getScope(); | ||
const { data } = yield axios__default['default'].post(`${config.host}/oauth/token`, { | ||
grant_type: 'client_credentials', | ||
client_id: config.clientId, | ||
client_secret: config.clientSecret, | ||
scope, | ||
}); | ||
/* istanbul ignore next */ | ||
const expires = Date.now() + (Number(data.expires_in) || 0) * 1000; | ||
cacheToken(data.access_token, expires); | ||
return { | ||
token: data.access_token, | ||
expires, | ||
}; | ||
}); | ||
const setMarket = (marketId) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -237,5 +288,10 @@ let markets; | ||
resetSession(); | ||
// Changing markets also invalidates the current guest token | ||
// Changing markets also invalidates the current token | ||
// so we need to get a new one ( or use the cached version ) | ||
yield loginAsGuest(); | ||
if (getTokenType() === 'integration') { | ||
yield loginAsIntegration(); | ||
} | ||
else { | ||
yield loginAsGuest(); | ||
} | ||
}); | ||
@@ -403,3 +459,3 @@ // Always return a copy so consumers cannot alter directly | ||
const baseRequest = getBaseRequest(); | ||
const { token } = isCustomerLoggedIn() ? getCustomerToken() : getToken(); | ||
const { token } = getCurrentToken(); | ||
return baseRequest.request({ | ||
@@ -697,2 +753,3 @@ method, | ||
logoutCustomer, | ||
loginAsIntegration, | ||
getToken, | ||
@@ -699,0 +756,0 @@ getCustomerToken, |
@@ -25,2 +25,3 @@ import axios from 'axios'; | ||
clientId: '', | ||
clientSecret: '', | ||
refreshTokens: true, | ||
@@ -37,2 +38,3 @@ refreshTokensAttempts: 5, | ||
clientId: defaultConfig.clientId, | ||
clientSecret: defaultConfig.clientSecret, | ||
refreshTokens: defaultConfig.refreshTokens, | ||
@@ -49,3 +51,4 @@ refreshTokensAttempts: defaultConfig.refreshTokensAttempts, | ||
config.host = providedConfig.host; | ||
config.clientId = providedConfig.clientId; | ||
config.clientId = providedConfig.clientId || ''; | ||
config.clientSecret = providedConfig.clientSecret || ''; | ||
config.refreshTokens = !!providedConfig.refreshTokens; | ||
@@ -87,5 +90,2 @@ config.refreshTokensAttempts = Number(providedConfig.refreshTokensAttempts); | ||
} | ||
if (!config.clientId) { | ||
throw new Error('Missing client id'); | ||
} | ||
if (isNaN(config.refreshTokensAttempts)) { | ||
@@ -123,2 +123,33 @@ config.refreshTokensAttempts = defaultConfig.refreshTokensAttempts; | ||
let tokenType = 'sales_channel'; | ||
const setTokenType = (type) => { | ||
tokenType = type; | ||
}; | ||
const getTokenType = () => tokenType; | ||
const tokenCache = new Map(); | ||
const INTEGRATION_PREFIX = 'integration__'; | ||
const cacheKey = () => { | ||
let prefix = ''; | ||
if (tokenType === 'integration') { | ||
prefix = INTEGRATION_PREFIX; | ||
} | ||
return prefix + getCurrentMarkets().join(','); | ||
}; | ||
const cacheToken = (token, expires) => { | ||
tokenCache.set(cacheKey(), { | ||
token, | ||
expires, | ||
}); | ||
}; | ||
const getToken = () => { | ||
const key = cacheKey(); | ||
const cachedValue = tokenCache.get(key); | ||
if (!cachedValue || !cachedValue.token || cachedValue.expires <= Date.now()) { | ||
return { | ||
token: '', | ||
expires: 0, | ||
}; | ||
} | ||
return cachedValue; | ||
}; | ||
const currentCustomerData = { | ||
@@ -163,22 +194,5 @@ id: '', | ||
.join(' '); | ||
const tokenCache = new Map(); | ||
const cacheKey = () => getCurrentMarkets().join(','); | ||
const cacheToken = (token, expires) => { | ||
tokenCache.set(cacheKey(), { | ||
token, | ||
expires, | ||
}); | ||
const getCurrentToken = () => { | ||
return isCustomerLoggedIn() ? getCustomerToken() : getToken(); | ||
}; | ||
const getToken = () => { | ||
const key = cacheKey(); | ||
const cachedValue = tokenCache.get(key); | ||
if (!cachedValue || !cachedValue.token || cachedValue.expires <= Date.now()) { | ||
return { | ||
token: '', | ||
expires: 0, | ||
}; | ||
} | ||
return cachedValue; | ||
}; | ||
@@ -190,2 +204,5 @@ const loginAsGuest = () => __awaiter(void 0, void 0, void 0, function* () { | ||
} | ||
if (!config.clientId) { | ||
throw new Error('Missing client id'); | ||
} | ||
const scope = getScope(); | ||
@@ -195,2 +212,3 @@ if (!scope) { | ||
} | ||
setTokenType('sales_channel'); | ||
// Check if a token is already available | ||
@@ -216,2 +234,35 @@ const cachedValue = getToken(); | ||
const loginAsIntegration = () => __awaiter(void 0, void 0, void 0, function* () { | ||
const config = getConfig(); | ||
if (!config.host) { | ||
throw new Error('You must call "init" before using any Auth method'); | ||
} | ||
if (!config.clientId) { | ||
throw new Error('Missing client id'); | ||
} | ||
if (!config.clientSecret) { | ||
throw new Error('Missing client secret'); | ||
} | ||
setTokenType('integration'); | ||
// Check if a token is already available | ||
const cachedValue = getToken(); | ||
if (cachedValue.token) { | ||
return cachedValue; | ||
} | ||
const scope = getScope(); | ||
const { data } = yield axios.post(`${config.host}/oauth/token`, { | ||
grant_type: 'client_credentials', | ||
client_id: config.clientId, | ||
client_secret: config.clientSecret, | ||
scope, | ||
}); | ||
/* istanbul ignore next */ | ||
const expires = Date.now() + (Number(data.expires_in) || 0) * 1000; | ||
cacheToken(data.access_token, expires); | ||
return { | ||
token: data.access_token, | ||
expires, | ||
}; | ||
}); | ||
const setMarket = (marketId) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -229,5 +280,10 @@ let markets; | ||
resetSession(); | ||
// Changing markets also invalidates the current guest token | ||
// Changing markets also invalidates the current token | ||
// so we need to get a new one ( or use the cached version ) | ||
yield loginAsGuest(); | ||
if (getTokenType() === 'integration') { | ||
yield loginAsIntegration(); | ||
} | ||
else { | ||
yield loginAsGuest(); | ||
} | ||
}); | ||
@@ -395,3 +451,3 @@ // Always return a copy so consumers cannot alter directly | ||
const baseRequest = getBaseRequest(); | ||
const { token } = isCustomerLoggedIn() ? getCustomerToken() : getToken(); | ||
const { token } = getCurrentToken(); | ||
return baseRequest.request({ | ||
@@ -689,2 +745,3 @@ method, | ||
logoutCustomer, | ||
loginAsIntegration, | ||
getToken, | ||
@@ -691,0 +748,0 @@ getCustomerToken, |
export interface InternalConfig { | ||
host: string; | ||
clientId: string; | ||
clientSecret: string; | ||
refreshTokens: boolean; | ||
@@ -14,3 +15,4 @@ refreshTokensAttempts: number; | ||
host: string; | ||
clientId: string; | ||
clientId?: string; | ||
clientSecret?: string; | ||
refreshTokens?: boolean; | ||
@@ -17,0 +19,0 @@ refreshTokensAttempts?: number; |
{ | ||
"name": "@atoms-studio/commercelayer-sdk", | ||
"version": "0.1.0-alpha.7", | ||
"version": "0.1.0-alpha.8", | ||
"repository": "git@github.com:atoms-studio/commercelayer-sdk.git", | ||
@@ -5,0 +5,0 @@ "author": "atoms.studio", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
169982
3448