question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Namespaces

See original GitHub issue

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…

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
FlorianWendelborncommented, Sep 20, 2021

Thanks @fabien0102

1reaction
fabien0102commented, Sep 8, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Namespaces
In Kubernetes, namespaces provides a mechanism for isolating groups of resources within a single cluster. Names of resources need to be ...
Read more >
Namespace - Wikipedia
In computing, a namespace is a set of signs (names) that are used to identify and refer to ... resources by isolated namespaces...
Read more >
Help:Namespaces
Namespaces are indicated in page titles by prefixing the page name with <namespace>: , so the prefix Help: in this page's title (...
Read more >
Working with Namespaces - AWS Cloud Map
When you create a namespace, you specify how you want to discover service instances that you register with AWS Cloud Map: using API...
Read more >
namespaces(7) - Linux manual page
A namespace wraps a global system resource in an abstraction that makes it appear to the processes within the namespace that they have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found