Skip to content

Commit

Permalink
Add empty department list fallback (#46)
Browse files Browse the repository at this point in the history
* empty departments

* docs(changeset): Add fallback so returns empty department list instead error
  • Loading branch information
cometkim authored Mar 19, 2024
1 parent a48c9a5 commit 5534a09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/purple-berries-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"greenhouse-jobboard-js": patch
---

Add fallback so returns empty department list instead error
22 changes: 21 additions & 1 deletion packages/greenhouse-jobboard-js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,16 @@ export class JobBoardClientV1 {
throw new Error(`Unexpected response type: ${JSON.stringify(data)}`);
}

return (data as any).departments;
if ('status' in data && data.status === 404) {
// If departments empty, Greenhouse treats it as "not found" error
return [];
}

if ('departments' in data) {
return data.departments as Array<Department & DepartmentListItem>;
}

throw new Error(`Unexpected response type: ${JSON.stringify(data)}`);
}

async getDepartmentTree(): Promise<Array<Department & DepartmentTreeNode>> {
Expand All @@ -107,6 +116,17 @@ export class JobBoardClientV1 {
throw new Error(`Unexpected response type: ${JSON.stringify(data)}`);
}

if ('status' in data && data.status === 404) {
// If departments empty, Greenhouse treats it as "not found" error
return [];
}

if ('departments' in data) {
return data.departments as Array<Department & DepartmentTreeNode>;
}

throw new Error(`Unexpected response type: ${JSON.stringify(data)}`);

return (data as any).departments;
}

Expand Down

0 comments on commit 5534a09

Please sign in to comment.