@forge/api
Advanced tools
Comparing version 0.5.2 to 0.5.3-next.0
# @forge/api | ||
## 0.5.3-next.0 | ||
### Patch Changes | ||
- d3ed280: GlobalStorage API now raises an APIError if response status code is not 200 | ||
- d3ed280: GlobalStorage will raise an APIError if response code is not 200 | ||
## 0.5.2 | ||
@@ -4,0 +11,0 @@ |
@@ -9,3 +9,3 @@ "use strict"; | ||
[413, 'Bad request'], | ||
[401, 'Permissions error or key does not exist'], | ||
[401, 'Authentication error'], | ||
[403, 'Permissions error or key does not exist'], | ||
@@ -30,2 +30,7 @@ [404, 'Permissions error or key does not exist'], | ||
}); | ||
describe('forUnexpected', () => { | ||
it('should return an APIError with a message provided', () => { | ||
expect(errors_1.APIError.forUnexpected('test').message).toEqual('test'); | ||
}); | ||
}); | ||
}); |
@@ -7,3 +7,4 @@ export declare const getErrorMessageFromCode: (code: string, message: string | null) => string; | ||
static forErrorCode(code: string, message: string | null): APIError; | ||
static forUnexpected(message: string): APIError; | ||
} | ||
//# sourceMappingURL=errors.d.ts.map |
@@ -12,2 +12,3 @@ "use strict"; | ||
case 401: | ||
return 'Authentication error'; | ||
case 403: | ||
@@ -34,3 +35,6 @@ case 404: | ||
} | ||
static forUnexpected(message) { | ||
return new APIError(message); | ||
} | ||
} | ||
exports.APIError = APIError; |
@@ -7,3 +7,3 @@ "use strict"; | ||
return jest.fn().mockReturnValue({ | ||
ok: statusCode !== 200 ? false : true, | ||
ok: statusCode === 200, | ||
status: statusCode, | ||
@@ -55,3 +55,3 @@ json: jest.fn().mockResolvedValue({ value: response }) | ||
}); | ||
it.each([401, 403, 404])('should throw a permissions error if the storage API responds with 401, 403 or 404', async (errorCode) => { | ||
it.each([403, 404])('should throw a permissions error if the storage API responds with 403 or 404', async (errorCode) => { | ||
const apiClientMock = getApiClientMock(undefined, errorCode); | ||
@@ -62,2 +62,8 @@ const productScopedStorage = getStorage(apiClientMock); | ||
}); | ||
it('should throw an authentication error if the storage API responds with 401', async () => { | ||
const apiClientMock = getApiClientMock(undefined, 401); | ||
const productScopedStorage = getStorage(apiClientMock); | ||
const response = productScopedStorage.set('testKey', 'testValue'); | ||
await expect(response).rejects.toThrow(/Authentication error/i); | ||
}); | ||
it('should throw a internal server error if the storage API responds with 500', async () => { | ||
@@ -91,2 +97,8 @@ const apiClientMock = getApiClientMock(undefined, 500); | ||
}); | ||
it('should throw an authentication error if the storage API responds with 401', async () => { | ||
const apiClientMock = getApiClientMock(undefined, 401); | ||
const productScopedStorage = getStorage(apiClientMock); | ||
const response = productScopedStorage.delete('testKey'); | ||
await expect(response).rejects.toThrow(/Authentication error/i); | ||
}); | ||
it('should throw an error if the storage API response is not ok', async () => { | ||
@@ -93,0 +105,0 @@ const apiClientMock = getApiClientMock(undefined, 400); |
@@ -9,3 +9,3 @@ "use strict"; | ||
return jest.fn().mockReturnValue({ | ||
ok: statusCode !== 200 ? false : true, | ||
ok: statusCode === 200, | ||
status: statusCode, | ||
@@ -17,3 +17,3 @@ text: jest.fn().mockResolvedValue(JSON.stringify(response)) | ||
return jest.fn().mockReturnValue({ | ||
ok: statusCode !== 200 ? false : true, | ||
ok: statusCode === 200, | ||
status: statusCode, | ||
@@ -81,6 +81,13 @@ text: jest.fn().mockResolvedValue(response) | ||
}); | ||
it('should throw an error with the returned error message for non 200 status codes', async () => { | ||
it('should throw an error with the returned status for non-200 status codes', async () => { | ||
const apiClientMock = getApiClientMock(undefined, 400); | ||
const globalStorage = getStorage(apiClientMock); | ||
const response = globalStorage.get('testKey'); | ||
expect(apiClientMock).toHaveBeenCalled(); | ||
await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400)); | ||
}); | ||
it('should throw an error with the returned error message for failed responses', async () => { | ||
const apiClientMock = getApiClientMock({ | ||
errors: [INVALID_CURSOR_ERROR] | ||
}, 400); | ||
}, 200); | ||
const globalStorage = getStorage(apiClientMock); | ||
@@ -92,3 +99,3 @@ const response = globalStorage.get('testKey'); | ||
it('should throw an error if the storage API returns a non 200 status code', async () => { | ||
const apiClientMock = getApiClientMockInvalidJson(`<xml></xml>`, 400); | ||
const apiClientMock = getApiClientMock(undefined, 400); | ||
const globalStorage = getStorage(apiClientMock); | ||
@@ -99,2 +106,9 @@ const response = globalStorage.get('testKey'); | ||
}); | ||
it('should throw an error if the response is not a valid JSON', async () => { | ||
const apiClientMock = getApiClientMockInvalidJson('test', 200); | ||
const globalStorage = getStorage(apiClientMock); | ||
const response = globalStorage.get('testKey'); | ||
expect(apiClientMock).toHaveBeenCalled(); | ||
await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test')); | ||
}); | ||
}); | ||
@@ -101,0 +115,0 @@ describe('set', () => { |
@@ -12,2 +12,5 @@ "use strict"; | ||
async function getResponseBody(response) { | ||
if (response.status !== 200) { | ||
throw errors_1.APIError.forStatus(response.status); | ||
} | ||
const responseText = await response.text(); | ||
@@ -19,3 +22,3 @@ let responseBody; | ||
catch (error) { | ||
throw errors_1.APIError.forStatus(response.status); | ||
throw errors_1.APIError.forUnexpected(`Response text was not a valid JSON: ${responseText}`); | ||
} | ||
@@ -22,0 +25,0 @@ assertNoErrors(responseBody.errors); |
{ | ||
"name": "@forge/api", | ||
"version": "0.5.2", | ||
"version": "0.5.3-next.0", | ||
"description": "Forge API methods", | ||
@@ -5,0 +5,0 @@ "author": "Atlassian", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73609
1413