Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function clearData(apiConfig, entityName, predicate = null) {
const client = createClient({
middlewares: [
createAuthMiddlewareForClientCredentialsFlow({ ...apiConfig, fetch }),
createHttpMiddleware({ host: apiConfig.apiUrl, fetch }),
],
})
let service = createRequestBuilder({
projectKey: apiConfig.projectKey,
})[entityName]
if (predicate) service = service.where(predicate)
const request = {
uri: service.build(),
method: 'GET',
}
return client.process(request, payload => {
// Built-in states cannot be deleted
const results =
entityName === 'states'
? payload.body.results.filter(state => state.builtIn === false)
: payload.body.results
export function getId(apiConfig, entityName) {
const client = createClient({
middlewares: [
createAuthMiddlewareForClientCredentialsFlow({ ...apiConfig, fetch }),
createHttpMiddleware({ host: apiConfig.apiUrl, fetch }),
],
})
const service = createRequestBuilder({
projectKey: apiConfig.projectKey,
})[entityName]
const request = {
uri: service.build(),
method: 'GET',
}
return client
.execute(request)
.then(result => Promise.resolve(result.body.results[0].id))
}
export function createData(apiConfig, entityName, data, id) {
const client = createClient({
middlewares: [
createAuthMiddlewareForClientCredentialsFlow({ ...apiConfig, fetch }),
createHttpMiddleware({ host: apiConfig.apiUrl, fetch }),
],
})
const requestOption = { projectKey: apiConfig.projectKey }
const service = createRequestBuilder(requestOption)[entityName]
return Promise.all(
data.map(_data => {
if (id) service.byId(id)
const request = {
uri: service.build(),
method: 'POST',
body: _data,
}
return client.execute(request)
})
)
}
static _createStateService(projectKey: string): ServiceBuilderInstance {
return createRequestBuilder({ projectKey }).states
}
createService() {
return createRequestBuilder({
projectKey: this.apiConfig.projectKey,
})[this.resource]
}
function _getCustomTypeDefinition(customTypeKey) {
const getTypeByKeyUri = createRequestBuilder({
projectKey: this.apiConfig.projectKey,
})
.types.where(`key="${customTypeKey}"`)
.build()
const execObj = {
uri: getTypeByKeyUri,
method: 'GET',
}
if (this.accessToken)
execObj.headers = {
Authorization: `Bearer ${this.accessToken}`,
}
return this.client.execute(execObj).then(response => {
if (response.body.count === 0)
return Promise.reject(
_createService(): ServiceBuilderInstance {
return createRequestBuilder({
projectKey: this.apiConfig.projectKey,
}).discountCodes
}
_createService(): ServiceBuilderInstance {
return createRequestBuilder({
projectKey: this.apiConfig.projectKey,
}).states
}