JohnAlbin / storybook-addon-fetch-mock

Adds fetch-mock to Storybook
Other
8 stars 5 forks source link

Export types #25

Open liamjones opened 2 months ago

liamjones commented 2 months ago

I want to make use of https://github.com/JohnAlbin/storybook-addon-fetch-mock/blob/a9367b1b096c6ffb08fb2e9947deef2f34f15ad3/src/typings.d.ts to type my stories but it doesn't look like it's exported from the module when published.

liamjones commented 2 months ago

Okay, it looks like they might not be 100% accurate for export purposes but it'd still be good to have some exported types.

They're probably not quite correct but here's what I'm using for now based on what's documented in the readme / what I'm seeing in a debugger:

declare module 'storybook-addon-fetch-mock' {
    import type { MockOptions, MockResponse, MockResponseFunction } from 'fetch-mock/types'

    export interface MockObject {
        matcher: Pick<MockOptions, 'url' | 'name' | 'method' | 'headers' | 'body' | 'matchPartialBody' | 'query' | 'params' | 'functionMatcher' | 'repeat'>;
        response?: MockOptions['response'];
        options?: Pick<MockOptions, 'delay' | 'sendAsJson' | 'includeContentLength'>;
    }

    export type WithFetchMockParameters = {
        parameters: {
            fetchMock: {
                mocks: MockObject[]
            }
        }
    }
}

This is allowing me to type my stories like this:

export const WithoutCarePlanOrTasks: Story & WithFetchMockParameters = {
    // ...
}