Skip to content

Commit

Permalink
Move LD client response to a function to remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedidomizio committed Nov 17, 2023
1 parent 0da3e83 commit 7db6472
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/app/api/_launch-darkly-promise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const LaunchDarklyApi = require('launchdarkly-api')

const handleLDApiClientResponse =
(
resolve: (_data: any | PromiseLike<any>) => void,
reject: (_error?: any) => void,
) =>
<T>(error: Error, data: T) => {
if (error) {
reject(error)
} else {
resolve(data)
}
}

export const _launchDarklyPromise = async <T>(
token: string,
ldApi: 'AccessTokensApi' | 'ProjectsApi',
Expand All @@ -20,23 +33,9 @@ export const _launchDarklyPromise = async <T>(
}

if (method === 'getProject') {
api[method](param, options, (error: Error, data: T) => {
if (error) {
console.log(error)
reject(error)
} else {
resolve(data)
}
})
api[method](param, options, handleLDApiClientResponse(resolve, reject))
}

api[method](param, (error: Error, data: T) => {
if (error) {
console.log(error)
reject(error)
} else {
resolve(data)
}
})
api[method](param, handleLDApiClientResponse(resolve, reject))
})
}

0 comments on commit 7db6472

Please sign in to comment.