Skip to content

Commit

Permalink
Merge pull request #17 from hopinc/feat/https-on-node-only
Browse files Browse the repository at this point in the history
  • Loading branch information
alii authored Sep 21, 2022
2 parents cf26482 + dcd458c commit a7f7363
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 106 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-ghosts-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@onehop/js': minor
---

Make await import('https') only run on node using conditional export
5 changes: 5 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Variables defined in tsup.config.ts
// Used to build specific code for specific runtimes
// while doing deadcode elimination

declare const TSUP_IS_NODE: boolean;
42 changes: 0 additions & 42 deletions gen-docs.mts

This file was deleted.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"module": "./dist/index.js",
"exports": {
".": {
"node": "./dist/node/index.js",
"browser": "./dist/index.js",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
Expand Down
10 changes: 6 additions & 4 deletions src/rest/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {fetch, Headers, Request} from '../util/fetch.js';
import {ExtractRouteParams} from '../util/index.js';
import {IS_BROWSER, IS_NODE} from '../util/constants.js';
import {IS_BROWSER} from '../util/constants.js';
import {createURLBuilder} from '../util/urls.js';
import {APIResponse, Endpoints, ErroredAPIResponse} from './endpoints.js';
import {getIdPrefix, Id, Method} from './types/index.js';
Expand Down Expand Up @@ -136,9 +136,11 @@ export class APIClient {
}

private async executeRequest<T>(request: Request): Promise<T> {
if (IS_NODE && !this.agent) {
const https = await import('https');
this.agent = new https.Agent({keepAlive: true});
if (TSUP_IS_NODE) {
if (!this.agent) {
const https = await import('https');
this.agent = new https.Agent({keepAlive: true});
}
}

const response = await fetch(request, {
Expand Down
10 changes: 0 additions & 10 deletions src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,4 @@ export const DEFAULT_BASE_URL = 'https://api.hop.io';
*/
export const IS_BROWSER = typeof window !== 'undefined';

/**
* If we are in Node.js.
*/
export const IS_NODE =
typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null &&
typeof Bun === 'undefined' &&
typeof Deno === 'undefined';

export const SUPPORTS_INTL = typeof Intl !== 'undefined';
73 changes: 23 additions & 50 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,35 @@
import {defineConfig} from 'tsup';
import {defineConfig, type Options} from 'tsup';

import glob from 'glob';
import fs from 'fs';

const utils = glob.sync('./src/utils/*');

/**
* Generate a package.json file for a util
*
* @param {string} utilName The name of the util to be exported
* @returns A package.json config
*/
function pkg(utilName: string) {
return JSON.stringify(
{
main: `../../dist/utils/${utilName}/index.js`,
module: `../../dist/utils/${utilName}/index.mjs`,
types: `../../dist/utils/${utilName}/index.d.ts`,
},
null,
4,
);
}

export default defineConfig({
entry: ['src/index.ts', 'src/utils/*/index.ts'],
const commonBuild: Options = {
splitting: true,
clean: true,
minify: false,
sourcemap: true,
dts: true,
format: ['cjs', 'esm'],
minifySyntax: true,
minifyWhitespace: true,
target: 'esnext',
define: {
TSUP_DEBUG: 'false',
banner: {
js: `/* Copyright ${new Date().getFullYear()} Hop, Inc */`,
},
onSuccess: async () => {
if (!fs.existsSync('./utils')) {
fs.mkdirSync('./utils');
}

for (const util of utils) {
const utilName = util.split('/').pop();
};

if (!utilName) {
continue;
}

const directory = `./utils/${utilName}`;

if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
const define = ({node = false} = {}) => ({
TSUP_IS_NODE: JSON.stringify(node),
});

fs.writeFileSync(`${directory}/package.json`, pkg(utilName), 'utf-8');
}
export default defineConfig([
{
...commonBuild,
entry: ['src/index.ts', 'src/utils/*/index.ts'],
define: define(),
},
banner: {
js: `/* Copyright ${new Date().getFullYear()} Hop, Inc */`,
{
...commonBuild,
entry: ['src/index.ts'],
outDir: 'dist/node',
define: define({
node: true,
}),
},
});
]);

1 comment on commit a7f7363

@vercel
Copy link

@vercel vercel bot commented on a7f7363 Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

hop-js – ./

hop-js.vercel.app
hop-js-onehop.vercel.app
hop-js-git-master-onehop.vercel.app
js.hop.io

Please sign in to comment.