Skip to content

Commit

Permalink
feat: esm & typedoc gen
Browse files Browse the repository at this point in the history
  • Loading branch information
alii committed Aug 30, 2022
1 parent 2051f45 commit 7a34a82
Show file tree
Hide file tree
Showing 31 changed files with 188 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
.DS_Store
dist
_workbench.ts
docs
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"yaml.schemas": {
"https://json.schemastore.org/github-action.json": "./.github/workflows/build.yml"
}
},
"typescript.tsdk": "node_modules/typescript/lib"
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"type": "module",
"repository": "https://github.com/hopinc/hop-js.git",
"author": "Hop <[email protected]>",
"license": "MIT",
Expand All @@ -22,7 +23,8 @@
"scripts": {
"build": "rm -rf dist && tsup",
"bench": "HOP_DEBUG=true ts-node _workbench.ts",
"release": "yarn build && yarn npm publish && rm -rf dist"
"release": "yarn build && yarn npm publish && rm -rf dist",
"vercel-build": "yarn typedoc src/index.ts"
},
"devDependencies": {
"@types/glob": "^7.2.0",
Expand All @@ -31,7 +33,8 @@
"prettier": "^2.7.1",
"ts-node": "^10.9.1",
"tsup": "^6.2.2",
"typescript": "^4.7.4"
"typedoc": "^0.23.11",
"typescript": "^4.8.2"
},
"dependencies": {
"@onehop/json-methods": "^1.1.1",
Expand Down
13 changes: 10 additions & 3 deletions src/hop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import {APIAuthentication, APIClient, APIClientOptions} from './rest/client';
import {channels, ignite, pipe, projects, registry, users} from './sdks';
import {DEFAULT_BASE_URL} from './util/constants';
import {APIAuthentication, APIClient, APIClientOptions} from './rest/client.js';
import {
channels,
ignite,
pipe,
projects,
registry,
users,
} from './sdks/index.js';
import {DEFAULT_BASE_URL} from './util/constants.js';

export type PartialAPIOptions = Partial<
Omit<APIClientOptions, 'authentication'>
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './hop';
export * from './permissions';
export * from './rest';
export * from './util';
export * from './hop.js';
export * from './permissions.js';
export * from './rest/index.js';
export * from './util/index.js';

// Commonly used in the API, so nice to export in two place
export {type APIAuthentication} from './rest';
export {ContainerState, RuntimeType} from './rest/types/ignite';
export {type APIAuthentication} from './rest/index.js';
export {ContainerState, RuntimeType} from './rest/types/ignite.js';
12 changes: 6 additions & 6 deletions src/rest/client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fetch, {Headers, Request} from 'cross-fetch';
import {ExtractRouteParams} from '../util';
import {IS_BROWSER} from '../util/constants';
import {createURLBuilder} from '../util/urls';
import {APIResponse, Endpoints, ErroredAPIResponse} from './endpoints';
import {getIdPrefix, Id, Method} from './types';
import {ExtractRouteParams} from '../util/index.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';

export type APIAuthentication = Id<'ptk'> | Id<'bearer'> | Id<'pat'>;

Expand Down Expand Up @@ -127,7 +127,7 @@ export class APIClient {
method: Method,
path: string,
body: unknown,
query: Record<string, string> = {},
query: Record<string, string | number | undefined> = {},
init: RequestInit = {},
) {
const url = this.url(path, query);
Expand Down
14 changes: 7 additions & 7 deletions src/rest/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Method} from './types';
import {ChannelEndpoints} from './types/channels';
import {IgniteEndpoints} from './types/ignite';
import {PipeEndpoints} from './types/pipe';
import {ProjectsEndpoints} from './types/projects';
import {RegistryEndpoints} from './types/registry';
import {UserEndpoints} from './types/users';
import {Method} from './types/index.js';
import {ChannelEndpoints} from './types/channels.js';
import {IgniteEndpoints} from './types/ignite.js';
import {PipeEndpoints} from './types/pipe.js';
import {ProjectsEndpoints} from './types/projects.js';
import {RegistryEndpoints} from './types/registry.js';
import {UserEndpoints} from './types/users.js';

export type SuccessfulAPIResponse<T> = {
success: true;
Expand Down
6 changes: 3 additions & 3 deletions src/rest/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './client';
export * from './endpoints';
export * from './types';
export * from './client.js';
export * from './endpoints.js';
export * from './types/index.js';
12 changes: 6 additions & 6 deletions src/rest/types/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Channels from './channels';
import * as Registry from './registry';
import * as Ignite from './ignite';
import * as Pipe from './pipe';
import * as Projects from './projects';
import * as Users from './users';
import * as Channels from './channels.js';
import * as Registry from './registry.js';
import * as Ignite from './ignite.js';
import * as Pipe from './pipe.js';
import * as Projects from './projects.js';
import * as Users from './users.js';

export {Pipe, Users, Projects, Ignite, Channels, Registry};
6 changes: 3 additions & 3 deletions src/rest/types/channels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Endpoint} from '../endpoints';
import {Project} from './projects';
import {Empty, Id, Timestamp} from '../../util/types';
import {Endpoint} from '../endpoints.js';
import {Project} from './projects.js';
import {Empty, Id, Timestamp} from '../../util/types.js';

export enum ChannelType {
PRIVATE = 'private',
Expand Down
6 changes: 3 additions & 3 deletions src/rest/types/ignite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ByteString} from '../../util';
import {Endpoint} from '../endpoints';
import {Empty, Id, Timestamp} from '../../util/types';
import {ByteString} from '../../util/index.js';
import {Endpoint} from '../endpoints.js';
import {Empty, Id, Timestamp} from '../../util/types.js';

export enum Regions {
US_EAST_1 = 'us-east-1',
Expand Down
4 changes: 2 additions & 2 deletions src/rest/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as API from './api';
export * from '../../util/types';
import * as API from './api.js';
export * from '../../util/types.js';
export {API};
6 changes: 3 additions & 3 deletions src/rest/types/pipe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Endpoint} from '../endpoints';
import {Regions} from './ignite';
import {Empty, Id, Timestamp} from '../../util/types';
import {Endpoint} from '../endpoints.js';
import {Regions} from './ignite.js';
import {Empty, Id, Timestamp} from '../../util/types.js';

export type DeliveryProtocol = 'webrtc' | 'hls';

Expand Down
6 changes: 3 additions & 3 deletions src/rest/types/projects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Endpoint} from '../endpoints';
import {Empty, Id, Timestamp} from '../../util/types';
import {User} from './users';
import {Endpoint} from '../endpoints.js';
import {Empty, Id, Timestamp} from '../../util/types.js';
import {User} from './users.js';

export type Member = Omit<User, 'email' | 'id'> & {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/rest/types/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Endpoint} from '../endpoints';
import {Empty} from '../../util/types';
import {Endpoint} from '../endpoints.js';
import {Empty} from '../../util/types.js';

export type RegistryEndpoints =
| Endpoint<'DELETE', '/v1/registry/images/:image', Empty>
Expand Down
6 changes: 3 additions & 3 deletions src/rest/types/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Endpoint} from '../endpoints';
import {MemberRole, Project} from './projects';
import {Empty, Id, Timestamp} from '../../util/types';
import {Endpoint} from '../endpoints.js';
import {MemberRole, Project} from './projects.js';
import {Empty, Id, Timestamp} from '../../util/types.js';

/**
* A user objct
Expand Down
7 changes: 5 additions & 2 deletions src/sdks/channels.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {create} from '@onehop/json-methods';
import {API, Id} from '../rest';
import {sdk} from './create';
import {API, Id} from '../rest/index.js';
import {sdk} from './create.js';

type Token = Id<'leap_token'>;

/**
* New state to set to a channel, or a callback function that will produce the new state
*/
export type SetStateAction<T extends API.Channels.State> =
| T
| ((oldState: T) => T | Promise<T>);
Expand Down
2 changes: 1 addition & 1 deletion src/sdks/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {APIClient} from '../rest';
import {APIClient} from '../rest/index.js';

export function sdk<T>(builder: (client: APIClient) => T) {
return builder;
Expand Down
8 changes: 4 additions & 4 deletions src/sdks/ignite.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {create, Infer} from '@onehop/json-methods';
import {API, assertId, Id} from '../rest';
import {Deployment, Gateway, GatewayType} from '../rest/types/ignite';
import {parseSize} from '../util';
import {sdk} from './create';
import {API, assertId, Id} from '../rest/index.js';
import {Deployment, Gateway, GatewayType} from '../rest/types/ignite.js';
import {parseSize} from '../util/index.js';
import {sdk} from './create.js';

const SIX_MB_IN_BYTES = 6 * 1024 * 1024;

Expand Down
12 changes: 6 additions & 6 deletions src/sdks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export {channels} from './channels';
export {ignite} from './ignite';
export {pipe} from './pipe';
export {projects} from './projects';
export {registry} from './registry';
export {users} from './users';
export {channels} from './channels.js';
export {ignite} from './ignite.js';
export {pipe} from './pipe.js';
export {projects} from './projects.js';
export {registry} from './registry.js';
export {users} from './users.js';
8 changes: 4 additions & 4 deletions src/sdks/pipe.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {create} from '@onehop/json-methods';
import {API, Id} from '../rest';
import {Regions} from '../rest/types/ignite';
import {DeliveryProtocol} from '../rest/types/pipe';
import {sdk} from './create';
import {API, Id} from '../rest/index.js';
import {Regions} from '../rest/types/ignite.js';
import {DeliveryProtocol} from '../rest/types/pipe.js';
import {sdk} from './create.js';

export const pipe = sdk(client => {
const Rooms = create<API.Pipe.Room>().methods({
Expand Down
4 changes: 2 additions & 2 deletions src/sdks/projects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Id} from '../rest';
import {sdk} from './create';
import {Id} from '../rest/index.js';
import {sdk} from './create.js';

export const projects = sdk(client => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/sdks/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Id} from '../rest';
import {sdk} from './create';
import {Id} from '../rest/index.js';
import {sdk} from './create.js';

export const registry = sdk(client => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/sdks/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Id} from '../util/types';
import {sdk} from './create';
import {Id} from '../util/types.js';
import {sdk} from './create.js';

export const users = sdk(client => ({
me: {
Expand Down
6 changes: 3 additions & 3 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './constants';
export * from './size';
export * from './types';
export * from './constants.js';
export * from './size.js';
export * from './types.js';
6 changes: 3 additions & 3 deletions src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export type Tag<T, Name extends string> = T & {
export type Timestamp = Tag<string, 'timestamp'>;

export type ExtractRouteParams<T extends string> = string extends T
? Record<string, string>
? Record<string, string | number | undefined>
: T extends `${string}:${infer Param}/${infer Rest}`
? {[k in Param | keyof ExtractRouteParams<Rest>]: string}
? {[k in Param | keyof ExtractRouteParams<Rest>]: string | number}
: T extends `${string}:${infer Param}`
? {[k in Param]: string}
? {[k in Param]: string | number}
: {};

export type Values<T> = T[keyof T];
Expand Down
14 changes: 12 additions & 2 deletions src/util/urls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ExtractRouteParams, Query} from '../rest';
import {ExtractRouteParams, Query} from '../rest/index.js';

export const lead = (x: string) => {
return x.charCodeAt(0) === 47 ? x : '/' + x;
Expand All @@ -16,6 +16,16 @@ export function join(a: string, b: string) {
return a + lead(b);
}

export function querystring(query: Query<string>) {
return Object.entries(query).reduce((acc, [key, value]) => {
if (value === undefined) {
return acc;
}

return acc + `${key}=${value.toString()}`;
}, '');
}

export function createURLBuilder(base: string) {
const regex = /:[^/?#]+/g;

Expand Down Expand Up @@ -45,7 +55,7 @@ export function createURLBuilder(base: string) {

const urlWithSearch = isObjectEmpty(query)
? urlWithQuery
: `${urlWithQuery}?${new URLSearchParams(query).toString()}`;
: `${urlWithQuery}?${querystring(query as Query<string>)}`;

return join(base, urlWithSearch);
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/zod/ids.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {z} from 'zod';
import {Id, IdPrefixes, validateId} from '../../rest';
import {Id, IdPrefixes, validateId} from '../../rest/index.js';

/**
* Generate a Zod schema for an ID prefix
Expand Down
2 changes: 1 addition & 1 deletion src/utils/zod/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './ids';
export * from './ids.js';
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"esModuleInterop": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react",
Expand Down
Loading

0 comments on commit 7a34a82

Please sign in to comment.