Skip to content

Commit

Permalink
feat: nicer error when no auth to Hop
Browse files Browse the repository at this point in the history
  • Loading branch information
alii committed Sep 2, 2022
1 parent 81572ac commit d2617bb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
42 changes: 42 additions & 0 deletions gen-docs.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import ts from 'typescript';
import {createReadStream} from 'node:fs';
import {json} from 'node:stream/consumers';

const tsconfig = (await json(createReadStream('./tsconfig.json'))) as {
compilerOptions: ts.CompilerOptions;
};

const program = ts.createProgram(['./src/index.ts'], {
...ts.getDefaultCompilerOptions(),
...tsconfig.compilerOptions,
moduleResolution: ts.ModuleResolutionKind.NodeNext,
module: ts.ModuleKind.NodeNext,
});

const printer = ts.createPrinter();
const hop = program.getSourceFile('./src/hop.ts');

if (!hop) {
throw new Error('Could not find Hop class file!');
}

const node = hop.forEachChild(node => {
if (ts.isClassDeclaration(node)) {
return node;
}
});

if (!node) {
throw new Error('Could not find Hop class!');
}

const members = node.members.filter(ts.isPropertyDeclaration);

const identifiers = members
.map(member => member.name)
.filter(Boolean)
.filter(ts.isIdentifier);

for (const identifier of identifiers) {
console.log(printer.printNode(ts.EmitHint.IdentifierName, identifier, hop));
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onehop/js",
"version": "1.3.10",
"version": "1.3.11",
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
Expand Down
11 changes: 10 additions & 1 deletion src/hop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ export class Hop {
public readonly channels;

constructor(options: PartialAPIOptions);

constructor(authentication: APIAuthentication, baseurl?: string);

constructor(
authenticationOrOptions: APIAuthentication | PartialAPIOptions,
baseUrl = DEFAULT_BASE_URL,
) {
if (
!authenticationOrOptions ||
(typeof authenticationOrOptions === 'object' &&
!authenticationOrOptions.authentication)
) {
throw new Error(
'Missing authentication token to `new Hop()` — please provide a valid Project Token, User Bearer or Personal Access Token',
);
}

this.client = new APIClient(
typeof authenticationOrOptions === 'object'
? {baseUrl: DEFAULT_BASE_URL, ...authenticationOrOptions}
Expand Down
8 changes: 4 additions & 4 deletions src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ export const ID_PREFIXES = [
prefix: 'leap_token',
description: 'Token for connecting to leap as a client',
},
{
prefix: "build",
description: "Build ID for build logs"
}
{
prefix: 'build',
description: 'Build ID for build logs',
},
] as const;

export type IdPrefixes = typeof ID_PREFIXES[number]['prefix'];
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"noFallthroughCasesInSwitch": true
},
"exclude": ["node_modules", "dist"],
"include": ["./**/*.ts", "./**/*.tsx"]
"include": ["./**/*.ts", "./**/*.tsx", "gen-docs.mts"]
}
8 changes: 4 additions & 4 deletions utils/zod/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"main": "../../dist/utils/zod/index.js",
"module": "../../dist/utils/zod/index.mjs",
"types": "../../dist/utils/zod/index.d.ts"
}
"main": "../../dist/utils/zod/index.js",
"module": "../../dist/utils/zod/index.mjs",
"types": "../../dist/utils/zod/index.d.ts"
}

1 comment on commit d2617bb

@vercel
Copy link

@vercel vercel bot commented on d2617bb Sep 2, 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-git-master-onehop.vercel.app
hop-js-onehop.vercel.app
js.hop.io

Please sign in to comment.