Skip to content

Commit

Permalink
feat(cli): api v2 configuration supports global header overrides (#4691)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Sep 18, 2024
1 parent c5be6ea commit 6df5775
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 9 deletions.
18 changes: 18 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
- changelogEntry:
- summary: |
The API V2 configuration (in beta) now supports global header
overrides. To specify global headers that are not in your
OpenAPI spec, simply add the following block in your `generators.yml`:
```yml generators.yml
api:
headers:
X-API-VERSION: string
specs:
- openapi: /path/to/openapi
overrides: /path/to/overrides
```
type: feat
irVersion: 53
version: 0.42.3

- changelogEntry:
- summary: Removes extraneous conditional error within namespacing configuration
type: fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ export interface GeneratorsConfiguration {

export type APIDefinition = SingleNamespaceAPIDefinition | MultiNamespaceAPIDefinition;

export interface SingleNamespaceAPIDefinition extends RawSchemas.WithEnvironmentsSchema, RawSchemas.WithAuthSchema {
export interface SingleNamespaceAPIDefinition
extends RawSchemas.WithEnvironmentsSchema,
RawSchemas.WithAuthSchema,
RawSchemas.WithHeadersSchema {
type: "singleNamespace";
definitions: APIDefinitionLocation[];
}

export interface MultiNamespaceAPIDefinition extends RawSchemas.WithEnvironmentsSchema, RawSchemas.WithAuthSchema {
export interface MultiNamespaceAPIDefinition
extends RawSchemas.WithEnvironmentsSchema,
RawSchemas.WithAuthSchema,
RawSchemas.WithHeadersSchema {
type: "multiNamespace";
rootDefinitions: APIDefinitionLocation[] | undefined;
definitions: Record<string, APIDefinitionLocation[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export const SpecSchema = z.union([OpenAPISpecSchema, AsyncAPISchema]);

export type SpecSchema = z.infer<typeof SpecSchema>;

export const APIConfigurationV2Schema = RawSchemas.WithEnvironmentsSchema.extend({
auth: z.optional(RawSchemas.ApiAuthSchema),
specs: z.array(SpecSchema)
});
export const APIConfigurationV2Schema = z
.object({
auth: z.optional(RawSchemas.ApiAuthSchema),
specs: z.array(SpecSchema)
})
.extend(RawSchemas.WithHeadersSchema.shape)
.extend(RawSchemas.WithEnvironmentsSchema.shape);
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ export const WithAuthSchema = z.strictObject({

export type WithAuthSchema = z.infer<typeof WithAuthSchema>;

export const WithHeadersSchema = z.strictObject({
headers: z.optional(z.record(z.string(), HttpHeaderSchema))
});

export type WithHeadersSchema = z.infer<typeof WithHeadersSchema>;

export const RootApiFileSchema = z
.strictObject({
name: z.string(), // TODO: should this be migrated to id?
imports: z.optional(z.record(z.string())),
headers: z.optional(z.record(z.string(), HttpHeaderSchema)),
"error-discrimination": z.optional(ErrorDiscriminationSchema),
audiences: z.optional(z.array(z.string())),
docs: z.optional(z.string()),
Expand All @@ -43,6 +48,7 @@ export const RootApiFileSchema = z
})
.extend(WithEnvironmentsSchema.shape)
.extend(WithAuthSchema.shape)
.extend(WithDisplayNameSchema.shape);
.extend(WithDisplayNameSchema.shape)
.extend(WithHeadersSchema.shape);

export type RootApiFileSchema = z.infer<typeof RootApiFileSchema>;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { DefinitionFileSchema } from "./DefinitionFileSchema";
export { PackageMarkerFileSchema } from "./PackageMarkerFileSchema";
export { RootApiFileSchema, WithEnvironmentsSchema, WithAuthSchema } from "./RootApiFileSchema";
export { RootApiFileSchema, WithEnvironmentsSchema, WithAuthSchema, WithHeadersSchema } from "./RootApiFileSchema";
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface OpenApiIrConverterContextOpts {
authOverrides?: RawSchemas.WithAuthSchema;

environmentOverrides?: RawSchemas.WithEnvironmentsSchema;

globalHeaderOverrides?: RawSchemas.WithHeadersSchema;
}

export class OpenApiIrConverterContext {
Expand All @@ -32,6 +34,7 @@ export class OpenApiIrConverterContext {
public builder: FernDefinitionBuilder;
public environmentOverrides: RawSchemas.WithEnvironmentsSchema | undefined;
public authOverrides: RawSchemas.WithAuthSchema | undefined;
public globalHeaderOverrides: RawSchemas.WithHeadersSchema | undefined;
public detectGlobalHeaders: boolean;
private defaultServerName: string | undefined = undefined;

Expand All @@ -41,6 +44,7 @@ export class OpenApiIrConverterContext {
enableUniqueErrorsPerEndpoint,
detectGlobalHeaders,
environmentOverrides,
globalHeaderOverrides,
authOverrides
}: OpenApiIrConverterContextOpts) {
this.logger = taskContext.logger;
Expand All @@ -50,6 +54,7 @@ export class OpenApiIrConverterContext {
this.detectGlobalHeaders = detectGlobalHeaders;
this.environmentOverrides = environmentOverrides;
this.authOverrides = authOverrides;
this.globalHeaderOverrides = globalHeaderOverrides;
}

public getSchema(id: SchemaId, namespace: string | undefined): Schema | undefined {
Expand Down
Loading

0 comments on commit 6df5775

Please sign in to comment.