这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/whole-parts-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: bundled context types
68 changes: 13 additions & 55 deletions dev/playground.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,14 @@
import { Sdk } from './.gen/sdk.gen';
import type { DefinePlugin, IR } from '@hey-api/openapi-ts';

const opencode = new Sdk();
opencode.session.create(
{
parentID: '',
title: '',
},
{
headers: {
'X-Custom-Header': 'value',
},
},
);
opencode.session.init({
id: '',
messageID: '',
modelID: '',
providerID: '',
});
opencode.session.chat({
agent: '',
id: '',
messageID: '',
modelID: '',
parts: [
{
name: '',
type: 'agent',
},
],
providerID: '',
system: '',
tools: {},
});
opencode.auth.set({
auth: {
// access: '',
// expires: 1,
key: '',
// refresh: '',
// token: '',
type: 'api',
},
id: '123',
});
opencode.postSessionByIdPermissionsByPermissionId({
id: 'session-id',
permissionID: 'permission-id',
response: 'always',
});
opencode.tui.showToast({
message: '',
title: '',
variant: 'error',
});
type MyPluginConfig = { readonly name: 'myplugin' };
type MyPlugin = DefinePlugin<MyPluginConfig>;

export function f(schema: IR.SchemaObject, plugin: MyPlugin['Instance']) {
plugin.context.resolveIrRef(schema.$ref);
}

export const handler: MyPlugin['Handler'] = ({ plugin }) => {
plugin.forEach('schema', 'operation', (event) => {
console.log(event);
});
};
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import colors from 'ansi-colors';
import { generateLegacyOutput } from '~/generate/legacy/output';
import { generateOutput } from '~/generate/output';
import { getSpec } from '~/getSpec';
import type { IR } from '~/ir/types';
import type { Context } from '~/ir/context';
import { parseLegacy, parseOpenApiSpec } from '~/openApi';
import { buildGraph } from '~/openApi/shared/utils/graph';
import { patchOpenApiSpec } from '~/openApi/shared/utils/patch';
Expand Down Expand Up @@ -254,7 +254,7 @@ export const createClient = async ({
* Always undefined on the first run, defined on subsequent runs.
*/
watches?: ReadonlyArray<WatchValues>;
}): Promise<Client | undefined | IR.Context> => {
}): Promise<Client | undefined | Context> => {
const watches: ReadonlyArray<WatchValues> =
_watches ||
Array.from({ length: config.input.length }, () => ({
Expand Down Expand Up @@ -296,7 +296,7 @@ export const createClient = async ({
).filter((data) => data.arrayBuffer || data.resolvedInput);

let client: Client | undefined;
let context: IR.Context | undefined;
let context: Context | undefined;

if (specData.length) {
const refParser = new $RefParser();
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
printCrashReport,
shouldReportCrash,
} from '~/error';
import type { IR } from '~/ir/types';
import type { Context } from '~/ir/context';
import type { Client } from '~/types/client';
import type { UserConfig } from '~/types/config';
import type { LazyOrAsync, MaybeArray } from '~/types/utils';
Expand All @@ -27,7 +27,7 @@ import { Logger } from '~/utils/logger';
export const createClient = async (
userConfig?: LazyOrAsync<MaybeArray<UserConfig>>,
logger = new Logger(),
): Promise<ReadonlyArray<Client | IR.Context>> => {
): Promise<ReadonlyArray<Client | Context>> => {
const resolvedConfig =
typeof userConfig === 'function' ? await userConfig() : userConfig;
const userConfigs = resolvedConfig
Expand Down Expand Up @@ -90,7 +90,7 @@ export const createClient = async (
}),
);
const result = clients.filter((client) => Boolean(client)) as ReadonlyArray<
Client | IR.Context
Client | Context
>;

eventCreateClient.timeEnd();
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/generate/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import path from 'node:path';

import type { ProjectRenderMeta } from '@hey-api/codegen-core';

import type { IR } from '~/ir/types';
import type { Context } from '~/ir/context';
import { getClientPlugin } from '~/plugins/@hey-api/client-core/utils';

import { generateClientBundle } from './client';
import { removeDirSync } from './utils';

export const generateOutput = async ({ context }: { context: IR.Context }) => {
export const generateOutput = async ({ context }: { context: Context }) => {
const outputPath = path.resolve(context.config.output.path);

if (context.config.output.clean) {
Expand Down
7 changes: 4 additions & 3 deletions packages/openapi-ts/src/ir/__tests__/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it, vi } from 'vitest';
import { defaultPaginationKeywords } from '~/config/parser';
import type { Config } from '~/types/config';

import type { Context } from '../context';
import { operationPagination } from '../operation';
import { getPaginationKeywordsRegExp } from '../pagination';
import type { IR } from '../types';
Expand Down Expand Up @@ -92,7 +93,7 @@ describe('operationPagination', () => {
...(pagination ? { pagination: true } : {}),
});

const emptyContext = {} as IR.Context;
const emptyContext = {} as Context;

const baseOperationMeta = {
method: 'post' as const,
Expand Down Expand Up @@ -216,7 +217,7 @@ describe('operationPagination', () => {
});

it('resolves $ref and uses the resolved pagination property', () => {
const context: IR.Context = {
const context: Context = {
resolveIrRef: vi.fn().mockReturnValue({
properties: {
pagination: {
Expand All @@ -228,7 +229,7 @@ describe('operationPagination', () => {
},
type: 'object',
}),
} as unknown as IR.Context;
} as unknown as Context;

const operation: IR.OperationObject = {
...baseOperationMeta,
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-ts/src/ir/operation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Context } from './context';
import type { Pagination } from './pagination';
import {
hasParametersObjectRequired,
Expand Down Expand Up @@ -33,7 +34,7 @@ export const operationPagination = ({
context,
operation,
}: {
context: IR.Context;
context: Context;
operation: IR.OperationObject;
}): Pagination | undefined => {
const body = operation.body;
Expand Down
5 changes: 3 additions & 2 deletions packages/openapi-ts/src/ir/parameter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Context } from './context';
import type { Pagination } from './pagination';
import type { IR } from './types';

const getPaginationSchema = ({
context,
parameter,
}: {
context: IR.Context;
context: Context;
parameter: IR.ParameterObject;
}): IR.SchemaObject | undefined => {
if (!parameter.pagination) {
Expand Down Expand Up @@ -66,7 +67,7 @@ export const parameterWithPagination = ({
context,
parameters,
}: {
context: IR.Context;
context: Context;
parameters: IR.ParametersObject | undefined;
}): Pagination | undefined => {
if (!parameters) {
Expand Down
2 changes: 0 additions & 2 deletions packages/openapi-ts/src/ir/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
ServerObject,
} from '~/openApi/3.1.x/types/spec';

import type { Context as IRContext } from './context';
import type { IRMediaType } from './mediaType';

interface IRBodyObject {
Expand Down Expand Up @@ -221,7 +220,6 @@ interface IRModel {
export namespace IR {
export type BodyObject = IRBodyObject;
export type ComponentsObject = IRComponentsObject;
export type Context<Spec extends Record<string, any> = any> = IRContext<Spec>;
export type Model = IRModel;
export type OperationObject = IROperationObject;
export type ParameterObject = IRParameterObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';

import type { IR } from '../../../../ir/types';
import type { Context } from '~/ir/context';

import type { ParameterObject, SecuritySchemeObject } from '../../types/spec';
import { parsePathOperation } from '../operation';

Expand All @@ -27,7 +28,7 @@ const createContext = () =>
resolveRef: () =>
// Mock implementation
undefined,
}) as unknown as IR.Context;
}) as unknown as Context;

describe('operation', () => {
const context = createContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { describe, expect, it } from 'vitest';

import type { Context } from '~/ir/context';
import type { OpenApi } from '~/openApi/types';

import type { IR } from '../../../../ir/types';
import { parseServers } from '../server';

describe('parseServers', () => {
it('host + basePath + schemes', () => {
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = {
const context: Partial<Context<Partial<OpenApi.V2_0_X>>> = {
config: {
input: [
// @ts-expect-error
Expand All @@ -23,7 +23,7 @@ describe('parseServers', () => {
schemes: ['http', 'https'],
},
};
parseServers({ context: context as IR.Context });
parseServers({ context: context as Context });
expect(context.ir!.servers).toEqual([
{
url: 'http://foo.com/v1',
Expand All @@ -35,7 +35,7 @@ describe('parseServers', () => {
});

it('schemes + host', () => {
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = {
const context: Partial<Context<Partial<OpenApi.V2_0_X>>> = {
config: {
input: [
// @ts-expect-error
Expand All @@ -50,7 +50,7 @@ describe('parseServers', () => {
schemes: ['ws'],
},
};
parseServers({ context: context as IR.Context });
parseServers({ context: context as Context });
expect(context.ir!.servers).toEqual([
{
url: 'ws://foo.com',
Expand All @@ -59,7 +59,7 @@ describe('parseServers', () => {
});

it('host + basePath', () => {
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = {
const context: Partial<Context<Partial<OpenApi.V2_0_X>>> = {
config: {
input: [
// @ts-expect-error
Expand All @@ -74,7 +74,7 @@ describe('parseServers', () => {
host: 'foo.com',
},
};
parseServers({ context: context as IR.Context });
parseServers({ context: context as Context });
expect(context.ir!.servers).toEqual([
{
url: 'foo.com/v1',
Expand All @@ -83,7 +83,7 @@ describe('parseServers', () => {
});

it('host', () => {
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = {
const context: Partial<Context<Partial<OpenApi.V2_0_X>>> = {
config: {
input: [
// @ts-expect-error
Expand All @@ -97,7 +97,7 @@ describe('parseServers', () => {
host: 'foo.com',
},
};
parseServers({ context: context as IR.Context });
parseServers({ context: context as Context });
expect(context.ir!.servers).toEqual([
{
url: 'foo.com',
Expand All @@ -106,7 +106,7 @@ describe('parseServers', () => {
});

it('basePath', () => {
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = {
const context: Partial<Context<Partial<OpenApi.V2_0_X>>> = {
config: {
input: [
// @ts-expect-error
Expand All @@ -120,7 +120,7 @@ describe('parseServers', () => {
basePath: '/v1',
},
};
parseServers({ context: context as IR.Context });
parseServers({ context: context as Context });
expect(context.ir!.servers).toEqual([
{
url: '/v1',
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/openApi/2.0.x/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IR } from '~/ir/types';
import type { Context } from '~/ir/context';
import { buildResourceMetadata } from '~/openApi/shared/graph/meta';
import { transformOpenApiSpec } from '~/openApi/shared/transforms';
import type { State } from '~/openApi/shared/types/state';
Expand Down Expand Up @@ -28,7 +28,7 @@ import { validateOpenApiSpec } from './validate';
type PathKeys<T extends keyof PathsObject = keyof PathsObject> =
keyof T extends infer K ? (K extends `/${string}` ? K : never) : never;

export const parseV2_0_X = (context: IR.Context<OpenApiV2_0_X>) => {
export const parseV2_0_X = (context: Context<OpenApiV2_0_X>) => {
if (context.config.parser.validate_EXPERIMENTAL) {
const result = validateOpenApiSpec(context.spec, context.logger);
handleValidatorResult({ context, result });
Expand Down
7 changes: 4 additions & 3 deletions packages/openapi-ts/src/openApi/2.0.x/parser/operation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Context } from '~/ir/context';
import type { IR, IRBodyObject } from '~/ir/types';
import type { State } from '~/openApi/shared/types/state';
import { operationToId } from '~/openApi/shared/utils/operation';
Expand Down Expand Up @@ -51,7 +52,7 @@ const initIrOperation = ({
path,
state,
}: Pick<IR.OperationObject, 'method' | 'path'> & {
context: IR.Context;
context: Context;
operation: Operation;
state: State;
}): IR.OperationObject => {
Expand Down Expand Up @@ -87,7 +88,7 @@ const operationToIrOperation = ({
securitySchemesMap,
state,
}: Pick<IR.OperationObject, 'method' | 'path'> & {
context: IR.Context;
context: Context;
operation: Operation;
securitySchemesMap: Map<string, SecuritySchemeObject>;
state: State;
Expand Down Expand Up @@ -364,7 +365,7 @@ export const parsePathOperation = ({
securitySchemesMap,
state,
}: {
context: IR.Context;
context: Context;
method: Extract<
keyof PathItemObject,
'delete' | 'get' | 'head' | 'options' | 'patch' | 'post' | 'put' | 'trace'
Expand Down
Loading
Loading