Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add createTypedRouter for building statically typed api router #133

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

jorgenader
Copy link
Contributor

@jorgenader jorgenader commented Nov 25, 2023

Adding new helper to create statically typed Router.

Took inspiration from @reduxjs/toolkit/query for the builder setup.

Example:

import { createTypedRouter } from '@tg-resources/core';
import { FetchResource } from '@tg-resources/fetch';

interface Animal {
    id: number;
    name: string;
}

const api = createTypedRouter({
    resource: FetchResource,
    config: {
        apiRoot: '/api/v1', // Set api root
    },
    routerBuilder: (builder) => ({
        // types order: Kwargs, FetchResponse, PostPayload, PostResponse
        auth: builder.resource<null, { authenticated: boolean }>([
            '/headers',
            { headers: () => ({ auth: 'foo' }), withCredentials: true },
        ]),
        // types order: Kwargs, FetchResponse, PostPayload, PostResponse
        cats: builder.resource<null, Animal[], Animal, Animal>({
            apiEndpoint: '/cats',
        }),
        // types order: Kwargs, FetchResponse, PostPayload, PostResponse
        cat: builder.resource<{ pk: number }, Animal, Animal>('/cats/${pk}'),
        dogs: builder.router((dogBuilder) => ({
            // types order: Kwargs, FetchResponse, PostPayload, PostResponse
            list: dogBuilder.resource<
                null,
                Animal[],
                Omit<Animal, 'id'>,
                Animal
            >('/dogs/'),
            details: dogBuilder.resource<{ pk: number }, Animal, Animal>(
                '/dogs/${pk}'
            ),
        })),
    }),
});

@jorgenader jorgenader self-assigned this Nov 25, 2023
@jorgenader jorgenader requested a review from Jyrno42 as a code owner November 25, 2023 19:28
@coveralls
Copy link

coveralls commented Nov 25, 2023

Coverage Status

coverage: 97.123% (-0.2%) from 97.304%
when pulling 92019c0 on feat/new-router-builder
into 72f0c06 on master.

@jorgenader jorgenader marked this pull request as draft November 25, 2023 19:32
@jorgenader jorgenader marked this pull request as ready for review November 26, 2023 13:33
… typings

BREAKING CHANGES:
- moved fetch and post resource method effects is to separate methods matching pattern `...Effect`

Signed-off-by: Jorgen Ader <[email protected]>
Signed-off-by: Jorgen Ader <[email protected]>
Signed-off-by: Jorgen Ader <[email protected]>
Signed-off-by: Jorgen Ader <[email protected]>
@jorgenader jorgenader force-pushed the feat/new-router-builder branch from 92019c0 to 91a5121 Compare November 19, 2024 13:15
@jorgenader jorgenader force-pushed the feat/new-router-builder branch 2 times, most recently from fa37dd4 to d9b4269 Compare November 19, 2024 13:55
@jorgenader jorgenader changed the title feat: Add createResourceRouter for building statically typed api router feat: Add createTypedRouter for building statically typed api router Nov 19, 2024
@jorgenader jorgenader force-pushed the feat/new-router-builder branch from d9b4269 to f80775d Compare November 19, 2024 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants