Skip to content

Commit d1819ca

Browse files
feat: jwt client (#3)
* feat: jwt client * chore: make the JWT client server only * chore: pretest * chore: fern ignore * chore: make cached token configurable and added UT * chore: export error as wildcard * test: developer version * test: dev version * chore: fernignore * chore: more fernignore
1 parent d12373f commit d1819ca

File tree

9 files changed

+6925
-764
lines changed

9 files changed

+6925
-764
lines changed

.fernignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# Specify files that shouldn't be modified by Fern
22

3-
.github/workflows/ci.yml
3+
# documentation
4+
README.md
5+
6+
# custom workflow
7+
.github/workflows/ci.yml
8+
9+
# custom code
10+
src/wrapper
11+
src/index.ts
12+
src/browser/jsonwebtoken-stub.ts
13+
tests/unit/wrapper

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,34 @@ const client = new ReferralExchangeClient({
180180
});
181181
```
182182

183+
### JWT Authenticated Client
184+
185+
If you need to authenticate requests by signing short-lived JWTs locally, use the `ReferralExchangeJwtClient`. It
186+
accepts a PEM-encoded ES256 private key and the API key name to embed as the issuer claim. By default each request
187+
receives a freshly signed token (15 second TTL) in the `Authorization` header, but you can opt into caching with a
188+
refresh buffer to reuse tokens safely. When opting in, you must supply the refresh buffer explicitly so the SDK knows
189+
how early to rotate tokens.
190+
191+
```typescript
192+
import { ReferralExchangeJwtClient } from "@opengovsg/refx-ts-sdk";
193+
194+
const client = new ReferralExchangeJwtClient({
195+
privateKey: process.env.REFX_PRIVATE_KEY!,
196+
apiKeyName: process.env.REFX_API_KEY_NAME!,
197+
environment: "Production", // or pass baseUrl
198+
tokenCache: {
199+
// The refresh buffer (required when enabling caching) is how much time must remain
200+
// before we stop reusing a JWT so requests don't arrive after the token's exp timestamp.
201+
refreshBufferSeconds: 5,
202+
},
203+
});
204+
205+
const offerings = await client.offerings.list();
206+
```
207+
208+
Omit the `tokenCache` block (the default) to sign a new token for every request. When you opt in, you must specify
209+
`refreshBufferSeconds` to control how early the SDK rotates tokens.
210+
183211
## Contributing
184212

185213
While we value open-source contributions to this SDK, this library is generated programmatically.

0 commit comments

Comments
 (0)