Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: manage multiple test env #328

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ typings/

# dotenv environment variables file
.env
.env.test
.env*.local
.env.*
test/env/

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ build/
gen/
src/
node_modules/
test/

tsconfig.json
tsconfig.*.json
Expand Down
32 changes: 17 additions & 15 deletions test/spot.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

import commercelayer, { CommerceLayerStatic } from '../lib/cjs'
import { inspect } from 'util'
import commercelayer, { Tag } from '../src/index'
import getToken from './token'
import { error } from 'console'

const ENV = 'topfarmacia'

;(async () => {

(async () => {
const auth = await getToken('integration', ENV).catch(err => {
console.log(`${err.message}\n`)
process.exit()
})

const auth = await getToken('integration')
const accessToken = auth ? auth.accessToken : ''
const organization = process.env.CL_SDK_ORGANIZATION || 'sdk-test-org'

Expand All @@ -17,19 +20,18 @@ import { error } from 'console'
})

try {

const order = await cl.orders.retrieve('nlKhmzozJO')
console.log(order)

fetch("http://httpstat.us/429")
.then(res => {
console.log('RESPONSE')
console.log(res)
}).catch(err => {
console.log('ERROR')
console.log(err)
});

} catch (error: any) {
console.log(inspect(error, false, null, true))
console.log(error.message)
console.log('\n\n --xx ERROR xx--\n')
if (CommerceLayerStatic.isApiError(error)) console.log(error.errors)
else {
console.log(inspect(error, false, null, true))
console.log(error.message)
}
}

})()
26 changes: 16 additions & 10 deletions test/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,28 @@ export type AccessToken = {



const organization = process.env.CL_SDK_ORGANIZATION || ''
const domain = process.env.CL_SDK_DOMAIN
const clientId = process.env.CL_SDK_CLIENT_ID || ''
const clientSecret = process.env.CL_SDK_CLIENT_SECRET || ''
const scope = process.env.CL_SDK_SCOPE || ''
// const endpoint = `https://${organization.toLowerCase()}.${domain ? domain : 'commercelayer.io'}`


const endpoint = `https://${organization.toLowerCase()}.${domain ? domain : 'commercelayer.io'}`
export default async (type: TokenType, env?: string): Promise<AccessToken> => {

if (env) {
const cfg = dotenv.config({ path: `./test/env/.env.${env}`, override: true })
if (cfg.error) throw cfg.error
}

const organization = process.env.CL_SDK_ORGANIZATION || ''
const domain = process.env.CL_SDK_DOMAIN
const clientId = process.env.CL_SDK_CLIENT_ID || ''
const clientSecret = process.env.CL_SDK_CLIENT_SECRET || ''
const scope = process.env.CL_SDK_SCOPE || ''

export default async (type: TokenType): Promise<AccessToken> => {
switch (type) {
case 'integration': return getAccessToken({ slug: organization, clientId, clientSecret, scope })
case 'integration': return getAccessToken({ slug: organization, clientId, clientSecret, scope, domain })
case 'sales_channel':
default: return getAccessToken({ slug: organization, clientId, scope })
default: return getAccessToken({ slug: organization, clientId, scope, domain })
}

}


Expand All @@ -62,7 +68,7 @@ const getAccessToken = async (auth: AuthData): Promise<AccessToken> => {
clientId: auth.clientId,
clientSecret: auth.clientSecret,
slug: auth.slug,
domain: auth.domain,
domain: auth.domain || undefined,
scope
}

Expand Down
Loading