Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

10.7 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package apicontract

import (
	"encoding/json"
	"fmt"
	"sort"
	"strings"
)

func sortedKeys[V any](values map[string]V) []string {
	keys := make([]string, 0, len(values))
	for key := range values {
		keys = append(keys, key)
	}
	sort.Strings(keys)
	return keys
}

func quote(value string) string { data, _ := json.Marshal(value); return string(data) }

func (b *Bundle) typeScript(schema *Schema) string {
	if schema.Format == "binary" {
		return "Blob"
	}
	if schema.Ref != "" {
		return strings.TrimPrefix(schema.Ref, "#/$defs/")
	}
	if len(schema.Enum) > 0 {
		var values []string
		for _, value := range schema.Enum {
			data, _ := json.Marshal(value)
			values = append(values, string(data))
		}
		return strings.Join(values, " | ")
	}
	if len(schema.AnyOf) > 0 {
		var variants []string
		// 联合成员缺少的字段标记为可选 never,保留精确的属性存在性和判别能力。
		allProperties := map[string]bool{}
		objects := make([]*Schema, len(schema.AnyOf))
		for i, option := range schema.AnyOf {
			target := option
			if option.Ref != "" {
				target = b.Definitions[strings.TrimPrefix(option.Ref, "#/$defs/")]
			}
			if target == nil || target.Type != "object" {
				continue
			}
			objects[i] = target
			for key := range target.Properties {
				allProperties[key] = true
			}
		}
		for i, option := range schema.AnyOf {
			variant := b.typeScript(option)
			if objects[i] != nil {
				var absent []string
				for _, key := range sortedKeys(allProperties) {
					if _, exists := objects[i].Properties[key]; !exists {
						absent = append(absent, quote(key)+"?: never;")
					}
				}
				if len(absent) > 0 {
					variant = "(" + variant + " & { " + strings.Join(absent, " ") + " })"
				}
			}
			variants = append(variants, variant)
		}
		return strings.Join(variants, " | ")
	}
	switch schema.Type {
	case "integer", "number":
		return "number"
	case "null", "string", "boolean":
		return schema.Type
	case "array":
		if schema.MaxItems != nil && schema.MinItems == *schema.MaxItems {
			items := make([]string, schema.MinItems)
			for i := range items {
				items[i] = b.typeScript(schema.Items)
			}
			return "[" + strings.Join(items, ", ") + "]"
		}
		if schema.MinItems == 1 {
			item := b.typeScript(schema.Items)
			return "[" + item + ", ...Array<" + item + ">]"
		}
		return "Array<" + b.typeScript(schema.Items) + ">"
	case "object":
		var indexSignature string
		if additional, ok := schema.AdditionalProperties.(*Schema); ok {
			if additional.Ref == "#/$defs/JSONValue" {
				indexSignature = "{ [key: string]: JSONValue }"
			} else {
				indexSignature = "Record<string, " + b.typeScript(additional) + ">"
			}
		}
		if len(schema.Properties) == 0 {
			if indexSignature != "" {
				return indexSignature
			}
			return "Record<string, never>"
		}
		required := map[string]bool{}
		for _, key := range schema.Required {
			required[key] = true
		}
		var fields []string
		for _, key := range sortedKeys(schema.Properties) {
			optional := ""
			if !required[key] {
				optional = "?"
			}
			fields = append(fields, quote(key)+optional+": "+b.typeScript(schema.Properties[key])+";")
		}
		object := "{ " + strings.Join(fields, " ") + " }"
		if indexSignature != "" {
			return "(" + object + " & " + indexSignature + ")"
		}
		return object
	default:
		panic("unsupported TypeScript schema: " + schema.Type)
	}
}

func (b *Bundle) TypeScript(documentation map[string]string) []byte {
	var output strings.Builder
	output.WriteString("// 此文件由内核契约生成,请运行 pnpm run api:generate 更新。\n\n")
	for _, name := range sortedKeys(b.Definitions) {
		if name == "UnknownTransactionAction" {
			fmt.Fprintf(&output, "export type %s = string & { readonly __unknownTransactionAction: unique symbol };\n\n", name)
			continue
		}
		fmt.Fprintf(&output, "export type %s = %s;\n\n", name, b.typeScript(b.Definitions[name]))
	}
	for _, method := range []string{"GET", "POST"} {
		fmt.Fprintf(&output, "export interface API%sRoutes {\n", method)
		for _, endpoint := range b.Endpoints {
			if endpoint.Method != method {
				continue
			}
			if text := documentation[endpoint.Handler]; text != "" {
				output.WriteString("    /**\n")
				for line := range strings.SplitSeq(strings.ReplaceAll(text, "*/", "*\\/"), "\n") {
					if line == "" {
						output.WriteString("     *\n")
					} else {
						fmt.Fprintf(&output, "     * %s\n", line)
					}
				}
				output.WriteString("     */\n")
			}
			fmt.Fprintf(&output, "    %s: {\n        request: %s;\n        response: %s;\n        body: %s;\n",
				quote(endpoint.Path), b.typeScript(endpoint.Request), b.typeScript(endpoint.Response), quote(string(endpoint.Body)))
			authorization, _ := json.Marshal(endpoint.Authorization)
			fmt.Fprintf(&output, "        /** 服务器路由要求;资源权限与笔记本租约仍由内核校验 */\n        authorization?: readonly %s;\n", authorization)
			if endpoint.Output != "" {
				fmt.Fprintf(&output, "        output: %s;\n", quote(string(endpoint.Output)))
			}
			if endpoint.NoContent {
				output.WriteString("        noContent: true;\n")
			}
			if len(endpoint.EmptyResponseStatuses) > 0 {
				statuses, _ := json.Marshal(endpoint.EmptyResponseStatuses)
				fmt.Fprintf(&output, "        emptyResponseStatuses: %s;\n", statuses)
			}
			if len(endpoint.AdditionalErrorStatuses) > 0 {
				statuses, _ := json.Marshal(endpoint.AdditionalErrorStatuses)
				fmt.Fprintf(&output, "        additionalErrorStatuses: %s;\n", statuses)
			}
			if len(endpoint.ContentVariants) > 0 {
				variants, _ := json.Marshal(endpoint.ContentVariants)
				fmt.Fprintf(&output, "        contentVariants: %s;\n", variants)
			}
			if ws := endpoint.WebSocket; ws != nil {
				fmt.Fprintf(&output, "        websocket: { incoming: %s; outgoing: %s; failureStatus: %d;", b.typeScript(ws.Incoming), b.typeScript(ws.Outgoing), ws.FailureStatus)
				if ws.Raw != nil {
					raw, _ := json.Marshal(ws.Raw)
					fmt.Fprintf(&output, " raw: %s;", raw)
				}
				output.WriteString(" };\n")
			}
			if sse := endpoint.SSE; sse != nil {
				if sse.Raw != nil {
					raw, _ := json.Marshal(sse.Raw)
					fmt.Fprintf(&output, "        sse: { raw: %s; };\n", raw)
				} else {
					output.WriteString("        sse: { events: { ")
					for _, name := range sortedKeys(sse.Events) {
						fmt.Fprintf(&output, "%s: %s; ", quote(name), b.typeScript(sse.Events[name]))
					}
					output.WriteString("}; };\n")
				}
			}
			if proxy := endpoint.Proxy; proxy != nil {
				protocol, _ := json.Marshal(proxy)
				fmt.Fprintf(&output, "        proxy: %s;\n", protocol)
			}
			if service := endpoint.PluginService; service != nil {
				protocol, _ := json.Marshal(service)
				fmt.Fprintf(&output, "        pluginService: %s;\n", protocol)
			}
			output.WriteString("    };\n")
		}
		output.WriteString("}\n\n")
	}
	output.WriteString(fetchDeclarations)
	return []byte(output.String())
}

const fetchDeclarations = `// 传输层合成的错误独立于业务错误;普通回调只接收消息处理后保留的非负错误码。
export interface APITransportError {
    code: -401 | -403 | -404;
    msg: string;
    data: null;
}

export interface APIFetchFailure {
    code: number;
    msg: string;
    data: null;
}

export interface APILegacyResponse {
    code: number;
    msg: string;
    data?: any;
    cmd?: string;
    callback?: string;
    sid?: string;
    context?: any;
}

type APIContract = {request: unknown; response: unknown; body: string};
export interface APIFormData<Request> extends FormData {
    readonly apiRequest: Request;
}
type APIRequestArgs<C extends APIContract> = C["body"] extends "multipart" | "form"
    ? [data: APIFormData<C["request"]>]
    : C["body"] extends "raw"
    ? [data?: JSONValue | FormData | null]
    : C["body"] extends "json" | "structJSON"
    ? [data: C["request"]]
    : [data?: C["request"] | null];
type NonNegative<C extends number> = C extends C ? ` + "`${C}` extends `-${string}`" + ` ? never : C : never;
export type APICallbackResponse<R> = R extends {code: infer C extends number}
    ? NonNegative<C> extends never ? never : R & {code: NonNegative<C>}
    : never;

type APIDirectCallbackResponse<R> = R extends {code: number} ? APICallbackResponse<R> : R;

type APIEmptyResponse<C> = C extends {emptyResponseStatuses: ReadonlyArray<number>} ? "" : never;
type APIPostEmptyResponse<C> = C extends {emptyResponseStatuses: infer S extends ReadonlyArray<number>}
    ? Exclude<S[number], 401 | 403 | 404> extends never ? never : "" : never;

type APIPostTail<C extends APIContract> = [
    cb?: (response: (C extends {output: "binary" | "proxy" | "pluginService"} ? JSONValue : C extends {output: "directJSON"} ? APIDirectCallbackResponse<C["response"]> | (C extends {noContent: true} ? "" : never) : C extends {output: "sse"} ? string | APICallbackResponse<C["response"]> : APICallbackResponse<C["response"]>) | APIPostEmptyResponse<C>) => void,
    headers?: Record<string, string>,
    failCallback?: (response: APIFetchFailure) => void,
    signal?: AbortSignal,
    timeout?: number
];
type LegacyPostArgs<Legacy> = [
    data?: any,
    cb?: (response: Legacy) => void,
    headers?: Record<string, string>,
    failCallback?: (response: Legacy) => void,
    signal?: AbortSignal,
    timeout?: number
];
type APISyncTail = [headers?: Record<string, string>, process?: boolean, signal?: AbortSignal];

// 路径只从首参推导,已知路径不能因请求参数不匹配而选择宽松重载。
export type FetchPost<Legacy = APILegacyResponse> = <Path extends string>(
    url: Path,
    ...args: Path extends keyof APIPOSTRoutes
        ? [...APIRequestArgs<APIPOSTRoutes[Path]>, ...APIPostTail<APIPOSTRoutes[Path]>]
        : string extends Path ? LegacyPostArgs<Legacy> : never
) => Promise<void>;

export type FetchSyncPost<Legacy = APILegacyResponse> = <Path extends string>(
    url: Path,
    ...args: Path extends keyof APIPOSTRoutes
        ? [...APIRequestArgs<APIPOSTRoutes[Path]>, ...APISyncTail]
        : string extends Path ? [data?: any, ...tail: APISyncTail] : never
) => Promise<Path extends keyof APIPOSTRoutes
    ? APIPOSTRoutes[Path] extends {output: "binary" | "proxy" | "pluginService"} ? JSONValue : APIPOSTRoutes[Path]["response"] | APITransportError
    : Legacy>;

export type FetchGet<Legacy = APILegacyResponse | string> = <Path extends string>(
    url: Path,
    ...args: Path extends keyof APIGETRoutes
        ? [cb: (response: APIGETRoutes[Path] extends {output: "binary" | "proxy" | "pluginService"} ? JSONValue : APIGETRoutes[Path]["response"] | APIEmptyResponse<APIGETRoutes[Path]> | (APIGETRoutes[Path] extends {output: "websocket" | "sse"} ? string : never)) => void]
        : Path extends keyof APIPOSTRoutes ? never
        : [cb: (response: Legacy) => void]
) => void;
`