Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
test: that it sends the right headers
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 6, 2020
1 parent c986e5f commit e24b5f2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
18 changes: 0 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@types/jest": "^25.2.1",
"husky": "^4.2.3",
"jest": "^25.2.7",
"nock": "^12.0.3",
"semantic-release": "^17.0.4",
"ts-jest": "^25.3.1",
"ts-loader": "^6.2.2",
Expand Down
40 changes: 37 additions & 3 deletions src/createClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
import { createClient, Client } from './createClient'
import { createClient } from './createClient'

describe('API Client', () => {
let client: Client
it('can be instantiated', () => {
client = createClient({ apiKey: 'some-api-key' })
const client = createClient({ apiKey: 'some-api-key' })
expect(client).toBeDefined()
})
it('sends the right headers', async () => {
const fetchMock = jest.fn(() => ({
status: 200,
json: async () => ({
_object: '/api/response',
self:
'https://api.flexport.com/shipments?page=1&per=20&sort=id&direction=desc',
version: 2,
data: {
_object: '/api/collections/paginated',
prev: null,
next: null,
data: null,
},
error: null,
}),
}))
const client = createClient({
apiKey: 'some-api-key',
fetchImplementation: fetchMock,
})

await client.listAllShipments()
expect(fetchMock).toHaveBeenCalledWith(
'https://api.flexport.com/shipments',
{
method: 'GET',
headers: {
Authorization: 'Bearer some-api-key',
'Content-Type': 'application/json',
'Flexport-Version': 2,
},
},
)
})
})
11 changes: 9 additions & 2 deletions src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ const handleResponse = async <A extends ApiResponseObject>(
const get = <A extends ApiResponseObject>({
endpoint,
headers,
fetchImplementation,
}: {
endpoint: string
headers: object
fetchImplementation?: any
}) => async (resource: string): Promise<A> =>
handleResponse(
fetch(`${endpoint}/api/${resource}`, {
(fetchImplementation || fetch)(`${endpoint}/${resource}`, {
method: 'GET',
headers,
}),
Expand All @@ -43,14 +45,19 @@ const get = <A extends ApiResponseObject>({
export const createClient = ({
apiKey,
endpoint,
fetchImplementation,
}: {
apiKey: string
endpoint?: string
fetchImplementation?: any
}): Client => {
const authorizedGet = <A extends ApiResponseObject>(resource: string) => () =>
const authorizedGet = <A extends ApiResponseObject>(
resource: string,
) => async () =>
get<A>({
headers: headers({ apiKey }),
endpoint: endpoint?.replace(/\/$/, '') || 'https://api.flexport.com',
fetchImplementation,
})(resource)
return {
listAllShipments: authorizedGet<Page<Shipment>>('shipments'),
Expand Down

0 comments on commit e24b5f2

Please sign in to comment.