Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions node-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@

export type { Configuration, Context, Flags, Options, TaskName } from './types';

export interface ShareOptions {
userToken: string;
storyId?: string;
onUrl?: (url: string) => void;
onProgress?: (progress: number, total: number) => void;
onError?: (error: Error) => void;
abortSignal?: AbortSignal;
}

export interface ShareOutput {
shareUrl: string;
}

export type InitialContext = Omit<
AtLeast<
Context,
Expand Down Expand Up @@ -350,3 +363,47 @@

export { getConfiguration } from './lib/getConfiguration';
export { createLogger, Logger } from './lib/log';

export async function share(options: ShareOptions): Promise<ShareOutput> {

Check failure on line 367 in node-src/index.ts

View workflow job for this annotation

GitHub Actions / lint-and-test / lint-and-test

Missing JSDoc comment
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const getAbortError = () => {

Check failure on line 369 in node-src/index.ts

View workflow job for this annotation

GitHub Actions / lint-and-test / lint-and-test

Move arrow function 'getAbortError' to the outer scope
const error = new Error('Share aborted');
error.name = 'AbortError';
return error;
};
const throwIfAborted = () => {
if (options.abortSignal?.aborted) {
throw getAbortError();
}
};

try {
throwIfAborted();
await wait(500);
throwIfAborted();

const randomId = uuid();
const storyQuery = options.storyId ? `?path=/story/${encodeURIComponent(options.storyId)}` : '';
const shareUrl = `https://share.chromatic.com/${randomId}${storyQuery}`;

throwIfAborted();
options.onUrl?.(shareUrl);

const total = 10;
for (let i = 1; i <= total; i += 1) {

Check failure on line 393 in node-src/index.ts

View workflow job for this annotation

GitHub Actions / lint-and-test / lint-and-test

The variable `i` should be named `index`. A more descriptive name will do too
throwIfAborted();
await wait(300);
throwIfAborted();
options.onProgress?.(i, total);
}

return { shareUrl };
} catch (error) {
if ((error as Error).name === 'AbortError') {
throw error;
}

options.onError?.(error as Error);
throw error;
}
}
Loading