Skip to content

Commit

Permalink
fix: type error
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Nov 25, 2024
1 parent 9fbcc5a commit 1356b24
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 47 deletions.
22 changes: 13 additions & 9 deletions packages/fetch-links/links/authLink.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import {
ClientAuthOptions,
mergeHeaders,
modifyRequest,
openIntProxyLink,
} from '@opensdks/runtime'
import {Link} from '../link.js'
import type {UnionToIntersection} from 'type-fest'
import type {ClientAuthOptions} from '@opensdks/runtime'
import {mergeHeaders, modifyRequest, openIntProxyLink} from '@opensdks/runtime'
import type {Link} from '../link.js'

export function authLink(auth: ClientAuthOptions, baseUrl: string): Link {
if (!auth) {
type Indexify<T> = T & Record<string, undefined>
type AllUnionKeys<T> = keyof UnionToIntersection<{[K in keyof T]: undefined}>
type NonDiscriminatedUnion<T> = {
[K in AllUnionKeys<T> & string]: Indexify<T>[K]
}

export function authLink(_auth: ClientAuthOptions, baseUrl: string): Link {
if (!_auth) {
// No Op
return (req, next) => next(req)
}
const auth = _auth as NonDiscriminatedUnion<ClientAuthOptions>

if (auth.openInt) {
return openIntProxyLink(auth.openInt, baseUrl)
Expand Down
3 changes: 2 additions & 1 deletion packages/fetch-links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"dependencies": {},
"devDependencies": {
"axios": "^1.6.2",
"concurrently": "^8.2.2"
"concurrently": "^8.2.2",
"type-fest": "^4.28.0"
},
"publishConfig": {
"access": "public"
Expand Down
9 changes: 8 additions & 1 deletion packages/runtime/createClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ test('application/x-www-form-urlencoded bracket style', () => {
)
})

test('expect links array to have 2 elements by default', () => {
test('expect links array to have 1 elements by default', () => {
const client = createClient()
expect(client.links.length).toBe(1)
})

test('expect links array to have 2 elements when auth is present', () => {
const client = createClient({
auth: {basic: {username: 'user', password: 'pass'}},
})
expect(client.links.length).toBe(2)
})

Expand Down
12 changes: 7 additions & 5 deletions packages/runtime/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {flattenNestedObject} from './utils.js'
type _ClientOptions = NonNullable<Parameters<typeof _createClient>[0]>

export type ClientAuthOptions =
| {openInt?: OpenIntProxyLinkOptions}
| {openInt: OpenIntProxyLinkOptions}
/** to be passed as Authorization header as a bearer token, Should handle automatic refreshing */
| {oauth?: {accessToken: string; refreshToken?: string; expiresAt?: number}}
| {basic?: {username: string; password: string}}
| {oauth: {accessToken: string; refreshToken?: string; expiresAt?: number}}
| {basic: {username: string; password: string}}
/** non oauth / directly specifying bearer token */
| {bearer?: string}
| {bearer: string}

export interface ClientOptions extends _ClientOptions {
links?: Link[] | ((defaultLinks: Link[]) => Link[])
Expand All @@ -42,7 +42,9 @@ export function createClient<Paths extends {}>({
...clientOptions
}: ClientOptions = {}) {
const defaultLinks = [
authLink(clientOptions.auth ?? {}, clientOptions.baseUrl ?? ''),
...(clientOptions.auth
? [authLink(clientOptions.auth, clientOptions.baseUrl ?? '')]
: []),
fetchLink(),
]
const links =
Expand Down
Loading

0 comments on commit 1356b24

Please sign in to comment.