Fixed up ClientApi types

This commit is contained in:
Tom Coleman 2021-08-31 22:42:53 +10:00
parent 10e6545ca4
commit a09a5317a9
4 changed files with 139 additions and 15 deletions

View File

@ -136,7 +136,7 @@ export default class ClientApi<TFramework extends Framework> {
private csfExports: Record<Path, ModuleExports>;
private addons: ClientApiAddons<TFramework>;
private addons: ClientApiAddons<TFramework['storyResult']>;
// If we don't get passed modules so don't know filenames, we can
// just use numeric indexes
@ -228,7 +228,7 @@ export default class ClientApi<TFramework extends Framework> {
};
// what are the occasions that "m" is a boolean vs an obj
storiesOf = (kind: string, m?: NodeModule): StoryApi<TFramework> => {
storiesOf = (kind: string, m?: NodeModule): StoryApi<TFramework['storyResult']> => {
if (!kind && typeof kind !== 'string') {
throw new Error('Invalid or missing kind provided for stories, should be a string');
}
@ -271,7 +271,7 @@ export default class ClientApi<TFramework extends Framework> {
}
let hasAdded = false;
const api: StoryApi<TFramework> = {
const api: StoryApi<TFramework['storyResult']> = {
kind: kind.toString(),
add: () => api,
addDecorator: () => api,

View File

@ -9,6 +9,7 @@ import ClientApi, {
} from './ClientApi';
export * from '@storybook/store';
export * from './types';
export {
addArgsEnhancer,

132
lib/client-api/src/types.ts Normal file
View File

@ -0,0 +1,132 @@
import {
Addon,
StoryId,
StoryName,
StoryKind,
ViewMode,
StoryFn,
Parameters,
Args,
ArgTypes,
StoryApi,
DecoratorFunction,
LoaderFunction,
StoryContext,
} from '@storybook/addons';
import { Framework, StoryIdentifier, GlobalAnnotations } from '@storybook/csf';
import { StoryStore, HooksContext } from '@storybook/store';
export type {
SBType,
SBScalarType,
SBArrayType,
SBObjectType,
SBEnumType,
SBIntersectionType,
SBUnionType,
SBOtherType,
} from '@storybook/csf';
// NOTE: these types are really just here for back-compat. Many of them don't have much meaning
// Remove in 7.0
export interface ErrorLike {
message: string;
stack: string;
}
// Metadata about a story that can be set at various levels: global, for a kind, or for a single story.
export interface StoryMetadata {
parameters?: Parameters;
decorators?: DecoratorFunction[];
loaders?: LoaderFunction[];
}
export type ArgTypesEnhancer = (context: StoryContext) => ArgTypes;
export type ArgsEnhancer = (context: StoryContext) => Args;
export type StorySpecifier = StoryId | { name: StoryName; kind: StoryKind } | '*';
export interface StoreSelectionSpecifier {
storySpecifier: StorySpecifier;
viewMode: ViewMode;
singleStory?: boolean;
args?: Args;
globals?: Args;
}
export interface StoreSelection {
storyId: StoryId;
viewMode: ViewMode;
}
export type AddStoryArgs = StoryIdentifier & {
storyFn: StoryFn<any>;
parameters?: Parameters;
decorators?: DecoratorFunction[];
loaders?: LoaderFunction[];
};
export type StoreItem = StoryIdentifier & {
parameters: Parameters;
getDecorated: () => StoryFn<any>;
getOriginal: () => StoryFn<any>;
applyLoaders: () => Promise<StoryContext>;
runPlayFunction: () => Promise<any>;
storyFn: StoryFn<any>;
unboundStoryFn: StoryFn<any>;
hooks: HooksContext<Framework>;
args: Args;
initialArgs: Args;
argTypes: ArgTypes;
};
export type PublishedStoreItem = StoreItem & {
globals: Args;
};
export interface StoreData {
[key: string]: StoreItem;
}
export interface ClientApiParams {
storyStore: StoryStore<Framework>;
decorateStory?: GlobalAnnotations<Framework>['applyDecorators'];
noStoryModuleAddMethodHotDispose?: boolean;
}
export type ClientApiReturnFn<StoryFnReturnType> = (...args: any[]) => StoryApi<StoryFnReturnType>;
export type { StoryApi, DecoratorFunction };
export interface ClientApiAddon<StoryFnReturnType = unknown> extends Addon {
apply: (a: StoryApi<StoryFnReturnType>, b: any[]) => any;
}
export interface ClientApiAddons<StoryFnReturnType> {
[key: string]: ClientApiAddon<StoryFnReturnType>;
}
export interface GetStorybookStory {
name: string;
render: StoryFn;
}
export interface GetStorybookKind {
kind: string;
fileName: string;
stories: GetStorybookStory[];
}
// This really belongs in lib/core, but that depends on lib/ui which (dev) depends on app/react
// which needs this type. So we put it here to avoid the circular dependency problem.
export type RenderContextWithoutStoryContext = StoreItem & {
forceRender: boolean;
showMain: () => void;
showError: (error: { title: string; description: string }) => void;
showException: (err: Error) => void;
};
export type RenderContext = RenderContextWithoutStoryContext & {
storyContext: StoryContext;
};

View File

@ -1,4 +1,5 @@
import { ClientApi } from '@storybook/client-api';
import { StoryStore } from '@storybook/store';
import { toId } from '@storybook/csf';
import { start } from './start';
@ -6,19 +7,9 @@ export default {
start,
toId,
ClientApi,
// TODO -- back compat
// ConfigApi,
// StoryStore,
StoryStore,
};
export {
start,
toId,
ClientApi,
// TODO back compat
// ConfigApi,
// StoryStore,
};
export { start, toId, ClientApi, StoryStore };
export { inferArgTypes } from './inferArgTypes';