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

Support Namespaces #43

Closed
FlorianWendelborn opened this issue Sep 2, 2021 · 7 comments
Closed

Support Namespaces #43

FlorianWendelborn opened this issue Sep 2, 2021 · 7 comments
Assignees

Comments

@FlorianWendelborn
Copy link

FlorianWendelborn commented Sep 2, 2021

Feature description

We currently auto-generate TypeScript interfaces from openapi specs. The result of this looks something like this:

Input

export namespace BulkDownloadParts {
	export interface PathParameters {
		service_id: number
	}

	export interface RequestBody {
		/**
		 * Define For which kind of entity you want to download parts.
		 * Will fall back to OrderLine if not set explicitly
		 *
		 */
		lineType?: 'OrderLine' | 'QuoteLine' | 'RequestForQuoteLine'
		partIds: number[]
	}

	export interface Response {
		jwt: string
		url: string
	}
}

Output

1. Via namespace

Would probably ease implementation, but is deprecated (we only use namespaces to group types, not really for actual runtime code)

// Generated by ts-to-zod
import { z } from 'zod'

export namespace BulkDownloadParts {
	export const pathParametersSchema = z.object({
		service_id: z.number(),
	})

	export const requestBodySchema = z.object({
		lineType: z
			.union([
				z.literal('OrderLine'),
				z.literal('QuoteLine'),
				z.literal('RequestForQuoteLine'),
			])
			.optional(),
		partIds: z.array(z.number()),
	})

	export const responseSchema = z.object({
		jwt: z.string(),
		url: z.string(),
	})
}

2. Via Prefixing

// Generated by ts-to-zod
import { z } from 'zod'

export const bulkDownloadPartsPathParametersSchema = z.object({
	service_id: z.number(),
})

export const bulkDownloadPartsRequestBodySchema = z.object({
	lineType: z
		.union([
			z.literal('OrderLine'),
			z.literal('QuoteLine'),
			z.literal('RequestForQuoteLine'),
		])
		.optional(),
	partIds: z.array(z.number()),
})

export const bulkDownloadPartsResponseSchema = z.object({
	jwt: z.string(),
	url: z.string(),
})

3. Via objects

export const BulkDownloadParts = {
	pathParametersSchema: z.object({
		service_id: z.number(),
	}),
	requestBodySchema: z.object({
		lineType: z
			.union([
				z.literal('OrderLine'),
				z.literal('QuoteLine'),
				z.literal('RequestForQuoteLine'),
			])
			.optional(),
		partIds: z.array(z.number()),
	}),
	responseSchema: z.object({
		jwt: z.string(),
		url: z.string(),
	})
}

Personally, I’d prefer the prefixing approach as it doesn’t use the quasi-deprecated namespace feature and as the object-based approach is proabably nearly impossible to tree-shake and thus would drastically increase bundle sizes for big APIs. The one in question defines 1584 interfaces with most of them being a lot more complex than this example, so it would make a noticeable difference...

@fabien0102
Copy link
Owner

Hi @FlorianWendelborn, thanks to pointing this, indeed I’m not using namespace at all and I totally forgot about them ^^

Indeed prefixing seams to be the simplest for this, let’s do this!

@fabien0102 fabien0102 self-assigned this Sep 8, 2021
@fabien0102
Copy link
Owner

The output with my PR #44

// Generated by ts-to-zod
import { z } from "zod";

export const bulkDownloadPartsPathParametersSchema = z.object({
  service_id: z.number(),
});

export const bulkDownloadPartsRequestBodySchema = z.object({
  lineType: z
    .union([
      z.literal("OrderLine"),
      z.literal("QuoteLine"),
      z.literal("RequestForQuoteLine"),
    ])
    .optional(),
  partIds: z.array(z.number()),
});

export const bulkDownloadPartsResponseSchema = z.object({
  jwt: z.string(),
  url: z.string(),
});

@fabien0102
Copy link
Owner

Included in v1.5.0

@FlorianWendelborn
Copy link
Author

Thanks @fabien0102

@Amorites
Copy link

namespace is not to be deprecated microsoft/TypeScript#30994 😢

@fabien0102
Copy link
Owner

Hi @Amorites,

I didn’t get your last comment; this library is supported namespaces, don’t worry; you can see how this is processed inside the unit tests -> https://github.com/fabien0102/ts-to-zod/pull/44/files

@Amorites
Copy link

Thank you @fabien0102

I saw there is some content in this issue like:

1. Via namespace

Would probably ease implementation, but is deprecated (we only use namespaces to group types, not really for actual runtime code)

Personally, I’d prefer the prefixing approach as it doesn’t use the quasi-deprecated namespace feature

Just a reminder that namespace is not going to deprecate.

And I have created an issue #102 about the current implementation.

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

No branches or pull requests

3 participants