Skip to content

Commit 23cf3e8

Browse files
authored
Fix/fetch json handles 404 (#2)
* fix: handle 404 when loading json * release: v0.3.1
1 parent bb79497 commit 23cf3e8

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@weccoframework/i18n",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "A simple, minimalistic i18n framework for use in browser",
55
"license": "Apache-2.0",
66
"repository": {

src/jsonsource.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function fetchJsonByLocale(baseUrl: string, options?: RequestInit): JsonS
4848
}
4949
}
5050

51-
function fetchText(url: string, options?: RequestInit): Promise<string> {
51+
function fetchText(url: string, options?: RequestInit): Promise<string | undefined> {
5252
return fetch(url, {
5353
body: options?.body,
5454
cache: options?.cache,
@@ -68,6 +68,11 @@ function fetchText(url: string, options?: RequestInit): Promise<string> {
6868
if (response.status < 300) {
6969
return response.text()
7070
}
71+
72+
if (response.status === 404) {
73+
return Promise.resolve(undefined)
74+
}
75+
7176
throw `Got unexpected status code when loading '${url}': ${response.status} (${response.statusText})`
7277
})
7378
}

test/jsonsource.test.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,48 @@ describe("fetchJsonByLocale", () => {
6969
let url: string
7070
let requestInit: RequestInit
7171

72-
before(() => {
73-
global.fetch = function (u: string, i: RequestInit): Promise<Response> {
74-
url = u
75-
requestInit = i
76-
77-
return Promise.resolve(new ResponseMock(200, `{"foo": "bar"}`))
78-
}
79-
80-
source = fetchJsonByLocale("/test/path", {
81-
method: "POST",
72+
describe("found", () => {
73+
before(() => {
74+
global.fetch = function (u: string, i: RequestInit): Promise<Response> {
75+
url = u
76+
requestInit = i
77+
78+
return Promise.resolve(new ResponseMock(200, `{"foo": "bar"}`))
79+
}
80+
81+
source = fetchJsonByLocale("/test/path", {
82+
method: "POST",
83+
})
84+
})
85+
86+
it("should load de.json", async () => {
87+
const json = await source(new Locale("de"))
88+
expect(json).toBe(`{"foo": "bar"}`)
89+
expect(url).toBe("/test/path/de.json")
90+
expect(requestInit.method).toBe("POST")
8291
})
8392
})
8493

85-
it("should load de.json", async () => {
86-
const json = await source(new Locale("de"))
87-
expect(json).toBe(`{"foo": "bar"}`)
88-
expect(url).toBe("/test/path/de.json")
89-
expect(requestInit.method).toBe("POST")
94+
describe("not found", () => {
95+
before(() => {
96+
global.fetch = function (u: string, i: RequestInit): Promise<Response> {
97+
url = u
98+
requestInit = i
99+
100+
return Promise.resolve(new ResponseMock(404, ""))
101+
}
102+
103+
source = fetchJsonByLocale("/test/path", {
104+
method: "POST",
105+
})
106+
})
107+
108+
it("should load de.json", async () => {
109+
const json = await source(new Locale("de"))
110+
expect(json).toBeUndefined()
111+
expect(url).toBe("/test/path/de.json")
112+
expect(requestInit.method).toBe("POST")
113+
})
90114
})
91115
})
92116

0 commit comments

Comments
 (0)