MCPcopy Index your code
hub / github.com/anomalyco/opencode / onEndpoint

Function onEndpoint

packages/httpapi-codegen/src/index.ts:89–177  ·  view source on GitHub ↗
({ endpoint, errors, group, middleware })

Source from the content-addressed store, hash-verified

87 HttpApi.reflect(api, {
88 onGroup() {},
89 onEndpoint({ endpoint, errors, group, middleware }) {
90 if (options?.omitEndpoints?.has(endpoint.name)) return
91 const groupName = options?.groupNames?.[group.identifier] ?? group.identifier
92 const name = `${groupName}.${endpoint.name}`
93 const required = Array.from(middleware).find((item) => item.requiredForClient)
94 if (required !== undefined) {
95 throw new GenerationError({ reason: `Client middleware requires adapter: ${required.key}` })
96 }
97
98 const successSchemas = Array.from(endpoint.success)
99 if (successSchemas.length === 0) successSchemas.push(HttpApiSchema.NoContent)
100 if (successSchemas.length > 1) throw new GenerationError({ reason: `Multiple success schemas: ${name}` })
101
102 const params = normalizeTransport(endpoint.params, "params", endpoint, name)
103 const query = normalizeTransport(endpoint.query, "query", endpoint, name)
104 const headers = normalizeTransport(endpoint.headers, "headers", endpoint, name)
105 const sourcePayloads = Array.from(endpoint.payload.values()).flatMap(({ schemas }) => schemas)
106 if (sourcePayloads.length > 1) {
107 throw new GenerationError({ reason: `Multiple payload schemas: ${name}` })
108 }
109 const payloads = sourcePayloads.map((schema) => normalizeTransport(schema, "payload", endpoint, name)!)
110 const success = normalizeTransport(successSchemas[0], "success", endpoint, name)!
111 const errorSchemas = Array.from(errors).flatMap(([status, schemas]) =>
112 schemas.map((schema) => ({ status, ...normalizeTransport(schema, "error", endpoint, name)! })),
113 )
114 const inputs = [
115 ...inputFields(params?.schema, "params", name),
116 ...inputFields(query?.schema, "query", name),
117 ...inputFields(headers?.schema, "headers", name),
118 ...payloads.flatMap((item) => inputFields(item.schema, "payload", name)),
119 ]
120 const names = new Set<string>()
121 for (const field of inputs) {
122 if (names.has(field.name)) throw new GenerationError({ reason: `Input field collision: ${field.name}` })
123 names.add(field.name)
124 }
125
126 const schemaPaths: Array<readonly [string, Schema.Top]> = [
127 ...(params === undefined ? [] : [[`${name}.params`, params.schema] as const]),
128 ...(query === undefined ? [] : [[`${name}.query`, query.schema] as const]),
129 ...(headers === undefined ? [] : [[`${name}.headers`, headers.schema] as const]),
130 ...payloads.map((item) => [`${name}.payload`, item.schema] as const),
131 ...responseSchemas(success.schema, `${name}.success`),
132 ...errorSchemas.map((item) => [`${name}.error.${item.status}`, item.schema] as const),
133 ]
134 const effectPortable =
135 [params, query, headers, ...payloads, success, ...errorSchemas].every(
136 (item) => item?.effectPortable !== false,
137 ) && streamEffectPortable(success.schema)
138 if (effectPortable) {
139 for (const [path, schema] of schemaPaths) assertPortable(schema, path, portable)
140 }
141
142 endpoints.push({
143 group: groupName,
144 sourceGroup: group.identifier,
145 topLevel: group.topLevel,
146 endpoint,

Callers

nothing calls this directly

Calls 13

normalizeTransportFunction · 0.85
inputFieldsFunction · 0.85
responseSchemasFunction · 0.85
streamEffectPortableFunction · 0.85
assertPortableFunction · 0.85
isDataEnvelopeFunction · 0.85
clientEndpointNameFunction · 0.85
isStreamSchemaFunction · 0.85
pushMethod · 0.80
findMethod · 0.65
addMethod · 0.65
fromMethod · 0.45

Tested by

no test coverage detected