Skip to content

Commit

Permalink
Split browser and Node code
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-keller committed Mar 15, 2024
1 parent 0c9ed8c commit e541e2e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 82 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"feature flag"
],
"main": "lib/index.js",
"browser": {
"./lib/apiCall.js": "./lib/apiCallBrowser.js"
},
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc",
Expand Down
4 changes: 2 additions & 2 deletions src/TgglClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export class TgglClient<
this.loader = new DataLoader<Partial<TContext>, Partial<TFlags>>(
async (contexts) => {
try {
return await apiCall({
return (await apiCall({
url: this.url,
apiKey: this.apiKey,
body: contexts,
method: 'post',
})
})) as Promise<any>
} catch (error) {
throw new Error(
// @ts-ignore
Expand Down
127 changes: 49 additions & 78 deletions src/apiCall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { request } from 'http'
import http from 'http'
import https from 'https'

export const apiCall = ({
apiKey,
Expand All @@ -10,87 +11,57 @@ export const apiCall = ({
method: 'post' | 'get'
apiKey?: string | null
body?: any
}) => {
if (typeof fetch !== 'undefined') {
return fetch(url, {
method,
body: body ? JSON.stringify(body) : undefined,
headers:
body && apiKey
? {
'Content-Type': 'application/json',
'x-tggl-api-key': apiKey,
}
: body
? {
'Content-Type': 'application/json',
}
: apiKey
? {
'x-tggl-api-key': apiKey,
}
: {},
}).then(async (r) => {
if (r.ok) {
return r.json()
}

throw await r.json()
})
} else {
const httpModule: { request: typeof request } = url.startsWith('https')
? require(['h', 'ps'].join('tt'))
: require(['h', 'p'].join('tt'))
}): Promise<unknown> => {
const httpModule = url.startsWith('https') ? https : http

return new Promise((resolve, reject) => {
const postData = body ? JSON.stringify(body) : ''
return new Promise((resolve, reject) => {
const postData = body ? JSON.stringify(body) : ''

const req = httpModule.request(
url,
{
method,
headers:
body && apiKey
? {
'x-tggl-api-key': apiKey,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
}
: body
? {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
}
: apiKey
? {
'x-tggl-api-key': apiKey,
}
: {},
},
(res) => {
let data = ''
const req = httpModule.request(
url,
{
method,
headers:
body && apiKey
? {
'x-tggl-api-key': apiKey,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
}
: body
? {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
}
: apiKey
? {
'x-tggl-api-key': apiKey,
}
: {},
},
(res) => {
let data = ''

res.on('data', (chunk) => (data += chunk))
res.on('data', (chunk) => (data += chunk))

res.on('end', () => {
try {
if (res.statusCode !== 200) {
reject(JSON.parse(data))
} else {
resolve(JSON.parse(data))
}
} catch (error) {
reject(error)
res.on('end', () => {
try {
if (res.statusCode !== 200) {
reject(JSON.parse(data))
} else {
resolve(JSON.parse(data))
}
})
}
)

req.on('error', reject)
if (body) {
req.write(postData)
} catch (error) {
reject(error)
}
})
}
req.end()
})
}
)

req.on('error', reject)
if (body) {
req.write(postData)
}
req.end()
})
}
37 changes: 37 additions & 0 deletions src/apiCallBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const apiCall = ({
apiKey,
body,
url,
method,
}: {
url: string
method: 'post' | 'get'
apiKey?: string | null
body?: any
}): Promise<unknown> => {
return fetch(url, {
method,
body: body ? JSON.stringify(body) : undefined,
headers:
body && apiKey
? {
'Content-Type': 'application/json',
'x-tggl-api-key': apiKey,
}
: body
? {
'Content-Type': 'application/json',
}
: apiKey
? {
'x-tggl-api-key': apiKey,
}
: {},
}).then(async (r) => {
if (r.ok) {
return r.json()
}

throw await r.json()
})
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"compilerOptions": {
"target": "es5",
"declaration": true,
"declarationMap": true,
"declarationMap": false,
"module": "commonjs",
"lib": [
"DOM","es6"],
"allowJs": false,
"sourceMap": true,
"sourceMap": false,
"outDir": "lib",
"rootDir": "src",
"strict": true,
Expand Down

0 comments on commit e541e2e

Please sign in to comment.