|
| 1 | +# Ogp TypeScript Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fopengovsg%2Frefer-ts-sdk) |
| 4 | +[](https://www.npmjs.com/package/ogp-refx) |
| 5 | + |
| 6 | +The Ogp TypeScript library provides convenient access to the Ogp API from TypeScript. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```sh |
| 11 | +npm i -s ogp-refx |
| 12 | +``` |
| 13 | + |
| 14 | +## Reference |
| 15 | + |
| 16 | +A full reference for this library is available [here](./reference.md). |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Instantiate and use the client with the following: |
| 21 | + |
| 22 | +```typescript |
| 23 | +import { ReferralExchangeClient } from "ogp-refx"; |
| 24 | + |
| 25 | +const client = new ReferralExchangeClient({ environment: "YOUR_BASE_URL", apiKey: "YOUR_API_KEY" }); |
| 26 | +await client.formSgWebhooksControllerHandleDoctorNotesFormWebhook({ |
| 27 | + key: "value", |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +## Request And Response Types |
| 32 | + |
| 33 | +The SDK exports all request and response types as TypeScript interfaces. Simply import them with the |
| 34 | +following namespace: |
| 35 | + |
| 36 | +```typescript |
| 37 | +import { ReferralExchange } from "ogp-refx"; |
| 38 | + |
| 39 | +const request: ReferralExchange.EligibilityGetRequest = { |
| 40 | + ... |
| 41 | +}; |
| 42 | +``` |
| 43 | + |
| 44 | +## Exception Handling |
| 45 | + |
| 46 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 47 | +will be thrown. |
| 48 | + |
| 49 | +```typescript |
| 50 | +import { ReferralExchangeError } from "ogp-refx"; |
| 51 | + |
| 52 | +try { |
| 53 | + await client.formSgWebhooksControllerHandleDoctorNotesFormWebhook(...); |
| 54 | +} catch (err) { |
| 55 | + if (err instanceof ReferralExchangeError) { |
| 56 | + console.log(err.statusCode); |
| 57 | + console.log(err.message); |
| 58 | + console.log(err.body); |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Advanced |
| 64 | + |
| 65 | +### Additional Headers |
| 66 | + |
| 67 | +If you would like to send additional headers as part of the request, use the `headers` request option. |
| 68 | + |
| 69 | +```typescript |
| 70 | +const response = await client.formSgWebhooksControllerHandleDoctorNotesFormWebhook(..., { |
| 71 | + headers: { |
| 72 | + 'X-Custom-Header': 'custom value' |
| 73 | + } |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +### Retries |
| 78 | + |
| 79 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 80 | +as the request is deemed retriable and the number of retry attempts has not grown larger than the configured |
| 81 | +retry limit (default: 2). |
| 82 | + |
| 83 | +A request is deemed retriable when any of the following HTTP status codes is returned: |
| 84 | + |
| 85 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 86 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 87 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 88 | + |
| 89 | +Use the `maxRetries` request option to configure this behavior. |
| 90 | + |
| 91 | +```typescript |
| 92 | +const response = await client.formSgWebhooksControllerHandleDoctorNotesFormWebhook(..., { |
| 93 | + maxRetries: 0 // override maxRetries at the request level |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +### Timeouts |
| 98 | + |
| 99 | +The SDK defaults to a 60 second timeout. Use the `timeoutInSeconds` option to configure this behavior. |
| 100 | + |
| 101 | +```typescript |
| 102 | +const response = await client.formSgWebhooksControllerHandleDoctorNotesFormWebhook(..., { |
| 103 | + timeoutInSeconds: 30 // override timeout to 30s |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +### Aborting Requests |
| 108 | + |
| 109 | +The SDK allows users to abort requests at any point by passing in an abort signal. |
| 110 | + |
| 111 | +```typescript |
| 112 | +const controller = new AbortController(); |
| 113 | +const response = await client.formSgWebhooksControllerHandleDoctorNotesFormWebhook(..., { |
| 114 | + abortSignal: controller.signal |
| 115 | +}); |
| 116 | +controller.abort(); // aborts the request |
| 117 | +``` |
| 118 | + |
| 119 | +### Runtime Compatibility |
| 120 | + |
| 121 | +The SDK defaults to `node-fetch` but will use the global fetch client if present. The SDK works in the following |
| 122 | +runtimes: |
| 123 | + |
| 124 | +- Node.js 18+ |
| 125 | +- Vercel |
| 126 | +- Cloudflare Workers |
| 127 | +- Deno v1.25+ |
| 128 | +- Bun 1.0+ |
| 129 | +- React Native |
| 130 | + |
| 131 | +### Customizing Fetch Client |
| 132 | + |
| 133 | +The SDK provides a way for your to customize the underlying HTTP client / Fetch function. If you're running in an |
| 134 | +unsupported environment, this provides a way for you to break glass and ensure the SDK works. |
| 135 | + |
| 136 | +```typescript |
| 137 | +import { ReferralExchangeClient } from "ogp-refx"; |
| 138 | + |
| 139 | +const client = new ReferralExchangeClient({ |
| 140 | + ... |
| 141 | + fetcher: // provide your implementation here |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | +## Contributing |
| 146 | + |
| 147 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 148 | +Additions made directly to this library would have to be moved over to our generation code, |
| 149 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 150 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 151 | +an issue first to discuss with us! |
| 152 | + |
| 153 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments