-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(postal-code): add postal code endpoint (#530)
* feat(postal-code): add postal code endpoint * refactor(postal-code-lookup): use http handler and rename stuff * refactor(postal-code-lookup): adjust endpoint name
- Loading branch information
1 parent
0808666
commit 67115a5
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/collections/delivery/operations/postal-code-lookup.operation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { BlapiResponse, BlError } from "@boklisten/bl-model"; | ||
|
||
import { isNullish } from "../../../helper/typescript-helpers"; | ||
import { HttpHandler } from "../../../http/http.handler"; | ||
import { Operation } from "../../../operation/operation"; | ||
import { BlApiRequest } from "../../../request/bl-api-request"; | ||
|
||
type SimplifiedBringPostalCodeResponse = { | ||
postal_codes?: | ||
| [ | ||
{ | ||
city: string; | ||
}, | ||
] | ||
| []; | ||
validation_errors?: [ | ||
{ | ||
code: string; | ||
description: string; | ||
}, | ||
]; | ||
}; | ||
|
||
export interface PostalCodeLookupSpec { | ||
postalCode: string; | ||
} | ||
|
||
export function verifyPostalCodeLookupSpec( | ||
postalCodeSpec: unknown, | ||
): postalCodeSpec is PostalCodeLookupSpec { | ||
const m = postalCodeSpec as Record<string, unknown> | null | undefined; | ||
return !!m && typeof m["postalCode"] === "string"; | ||
} | ||
|
||
export class PostalCodeLookupOperation implements Operation { | ||
private httpHandler: HttpHandler; | ||
|
||
constructor(httpHandler?: HttpHandler) { | ||
this.httpHandler = httpHandler ? httpHandler : new HttpHandler(); | ||
} | ||
|
||
async run(blApiRequest: BlApiRequest): Promise<BlapiResponse> { | ||
const postalCodeLookupSpec = blApiRequest.data; | ||
if (!verifyPostalCodeLookupSpec(postalCodeLookupSpec)) { | ||
throw new BlError(`Malformed PostalCodeSpec`).code(701); | ||
} | ||
const bringAuthHeaders = { | ||
"X-MyBring-API-Key": process.env["BRING_API_KEY"], | ||
"X-MyBring-API-Uid": process.env["BRING_API_ID"], | ||
}; | ||
try { | ||
const response = (await this.httpHandler.getWithQuery( | ||
`https://api.bring.com/address/api/NO/postal-codes/${postalCodeLookupSpec.postalCode}`, | ||
"", | ||
bringAuthHeaders, | ||
)) as SimplifiedBringPostalCodeResponse; | ||
|
||
if ( | ||
response.validation_errors || | ||
isNullish(response.postal_codes) || | ||
response.postal_codes.length === 0 | ||
) { | ||
return new BlapiResponse([{ postalCity: null }]); | ||
} | ||
|
||
return new BlapiResponse([{ postalCity: response.postal_codes[0].city }]); | ||
} catch (error) { | ||
throw new BlError("failed to lookup postal code").data(error).code(200); | ||
} | ||
} | ||
} |