Skip to content
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ The files will be compiled under `dist` directory:
```bash
npm test
```

## Configuration

Set the following environment variables:

```bash
export JANE_CLIENT_ID="<provided by partner success>"
export JANE_CLIENT_SECRET="<provided by partner success>"
# optional, defaults to the following
export JANE_API_URL="https://api.iheartjane.com"
```
18 changes: 9 additions & 9 deletions examples/javascript/src/lib/api-service.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { request } from './http.mjs'
import { URL } from "url";

const headers = { 'Content-Type': 'application/json' }
const defaultHost = "https://api.iheartjane.com"

const authenticateClient = async () => {
const clientId = process.env.CLIENT_ID || ''
const clientSecret = process.env.CLIENT_SECRET || ''
const janeApiHost = process.env.JANE_API_HOST
const clientId = process.env.JANE_CLIENT_ID || ''
const clientSecret = process.env.JANE_CLIENT_SECRET || ''
const apiUrl = process.env.JANE_API_URL

const resp = await request({
method: 'post',
url: `${janeApiHost}/oauth/token`,
url: `${apiUrl}/oauth/token`,
grant_type: 'client_credentials'
}, {
auth: {
Expand All @@ -23,14 +24,13 @@ const authenticateClient = async () => {
}

const makeRequest = async (options) => {
const apiUrl = process.env.JANE_API_URL

const host = process.env.API_HOST

if (!host) {
throw Error('No API_HOST configured')
if (!apiUrl) {
throw Error('No JANE_API_URL configured')
}

const url = new URL(options.path, host);
const url = new URL(options.path, apiUrl);

try {
const response = await request({
Expand Down
8 changes: 4 additions & 4 deletions examples/javascript/src/lib/api-service.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ describe('api service', () => {
)
})

describe('if no API_HOST is set', () => {
describe('if no JANE_API_URL is set', () => {
test('it throws an error', async () => {
const token = 'someToken'

await expect(async () => {
await apiService.post('/api/path', {}, token)
}).rejects.toEqual(new Error('No API_HOST configured'))
}).rejects.toEqual(new Error('No JANE_API_URL configured'))
})
})

describe('#get', () => {
beforeEach(() => {
process.env.API_HOST = 'https://test.localhost'
process.env.JANE_API_URL = 'https://test.localhost'
})

test('it calls fetch with method "get"', async () => {
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('api service', () => {

describe('#post', () => {
beforeEach(() => {
process.env.API_HOST = 'https://test.localhost'
process.env.JANE_API_URL = 'https://test.localhost'
})

test('it calls fetch with method "post" and body data', async () => {
Expand Down