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

Enhance SDK random generator -> add simulate() functions #375

Merged
merged 1 commit into from
Jun 7, 2023

Conversation

samchon
Copy link
Owner

@samchon samchon commented Jun 7, 2023

Added independent simulate() function for convenience:

/**
 * Paginate entire articles.
 * 
 * @param section Section code
 * @param input Page request info
 * @returns Paginated articles with summarized info
 * 
 * @controller RandomController.index()
 * @path PATCH /random/:section
 * @nestia Generated by Nestia - https://github.com/samchon/nestia
 */
export async function index(
    connection: IConnection,
    section: null | string,
    input: Primitive<index.Input>,
): Promise<index.Output> {
    return !!connection.random
        ? index.simulate(
              connection,
              section,
              input,
          )
        : Fetcher.fetch(
              connection,
              index.ENCRYPTED,
              index.METHOD,
              index.path(section),
              input,
          );
}
export namespace index {
    export type Input = Primitive<IPage.IRequest>;
    export type Output = Primitive<IPage<IBbsArticle.ISummary>>;

    export const METHOD = "PATCH" as const;
    export const PATH: string = "/random/:section";
    export const ENCRYPTED: Fetcher.IEncrypted = {
        request: false,
        response: false,
    };

    export const path = (section: null | string): string => {
        return `/random/${encodeURIComponent(section ?? "null")}`;
    }
    export const random = (g?: Partial<typia.IRandomGenerator>): Output =>
        typia.random<Output>(g);
    export const simulate = async (
        connection: IConnection,
        section: null | string,
        input: IPage.IRequest,
    ): Promise<Output> => {
        const assert =
            (message: (exp: typia.TypeGuardError) => string) =>
            <T>(task: () => T): void => {
                try {
                    task()
                }
                catch (exp) {
                    if (typia.is<typia.TypeGuardError>(exp))
                        throw new HttpError(
                            METHOD,
                            `${connection.host}${path(section)}`,
                            400,
                            JSON.stringify({
                                method: exp.method,
                                path: exp.path,
                                expected: exp.expected,
                                value: exp.value,
                                message: message(exp),
                            })
                        );
                    throw exp
                }
            };
        assert(
            () => "URL parameter \"section\" is not ${exp.expected} type."
        )(() => typia.assert(section));
        assert(
            () => "Request body is not following the promised type."
        )(() => typia.assert(input));
        return typia.random<Output>(
            typeof connection.random === 'object'
            && connection.random !== null
                ? connection.random
                : undefined
        );
    }
}

@samchon samchon added the enhancement New feature or request label Jun 7, 2023
@samchon samchon self-assigned this Jun 7, 2023
@samchon samchon merged commit 1c5e0f3 into master Jun 7, 2023
@samchon samchon deleted the features/simulate branch June 7, 2023 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant