@aceandtate/clicktoaddress
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -25,3 +25,7 @@ "use strict"; | ||
const response = yield fetch(url, requestOptions); | ||
return response.json(); | ||
const json = yield response.json(); | ||
if (json.error) { | ||
throw json.error.message || json.error; | ||
} | ||
return json; | ||
}); | ||
@@ -28,0 +32,0 @@ const countries = (args) => api("countries", args).then((data) => data.countries.map(country => (Object.assign({}, country, { get: (query) => find({ country: country.code, query }) })))); |
{ | ||
"name": "@aceandtate/clicktoaddress", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "A clicktoaddress client", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import createClient from "."; | ||
const key = "some key"; | ||
const fetchWithMock = (response: object = {}) => | ||
jest.fn().mockResolvedValue({ json: () => response }); | ||
const clientWithMock = (options?: object, response?: any) => | ||
createClient({ | ||
key: "some key", | ||
fetch: fetchWithMock(response), | ||
...options | ||
}); | ||
describe("createClient", () => { | ||
it("should return a set of functions", () => { | ||
const { get, countries, find, retrieve } = createClient({ key }); | ||
const { get, countries, find, retrieve } = clientWithMock(); | ||
expect(get).toBeInstanceOf(Function); | ||
@@ -14,6 +23,5 @@ expect(countries).toBeInstanceOf(Function); | ||
it("should call the passed fetch", async () => { | ||
const fetch = jest.fn().mockResolvedValue({ json: () => "response" }); | ||
const { get } = createClient({ fetch, key }); | ||
const fetch = fetchWithMock(); | ||
const { get } = clientWithMock({ fetch }); | ||
const response = await get("something", { my: "arg" }); | ||
expect(response).toBe("response"); | ||
expect(fetch).toHaveBeenCalledWith( | ||
@@ -29,2 +37,9 @@ "https://api.craftyclicks.co.uk/address/1.1/something", | ||
}); | ||
it("should reject with the result of the API error, if available", async () => { | ||
const { get } = clientWithMock(null, { | ||
error: { message: "error message" } | ||
}); | ||
await expect(get("something", {})).rejects.toMatch("error message"); | ||
}); | ||
}); |
@@ -103,3 +103,10 @@ import * as isomorphicFetch from "isomorphic-fetch"; | ||
const response = await fetch(url, requestOptions); | ||
return response.json(); | ||
const json = await response.json(); | ||
if (json.error) { | ||
// Stacktrace not useful in this case, | ||
// so we throw an error string | ||
throw json.error.message || json.error; | ||
} | ||
return json; | ||
}; | ||
@@ -106,0 +113,0 @@ |
Sorry, the diff of this file is not supported yet
16831
349