index.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*!
  2. * TSBuffer Schema v2.2.0
  3. * -----------------------------------------
  4. * MIT LICENSE
  5. * KingWorks (C) Copyright 2022
  6. * https://github.com/k8w/tsbuffer-schema
  7. */
  8. /**
  9. * TypeScript `any` type
  10. *
  11. * @remarks
  12. * See: {@link https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any}
  13. *
  14. * @example
  15. * ```ts
  16. * type XXX = any;
  17. * ```
  18. */
  19. export declare interface AnyTypeSchema {
  20. type: 'Any';
  21. }
  22. /**
  23. * TypeScript `Array` type
  24. *
  25. * @remarks
  26. * See: {@link https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#arrays}
  27. *
  28. * @example
  29. * ```ts
  30. * type A = string[];
  31. * type B = Array<A>;
  32. * ```
  33. */
  34. export declare interface ArrayTypeSchema {
  35. type: 'Array';
  36. elementType: TSBufferSchema;
  37. }
  38. /** Encoded as 64-bit `int64` */
  39. export declare type bigint64 = bigint;
  40. /** Encoded as 64-bit `uint64` */
  41. export declare type biguint64 = bigint;
  42. /**
  43. * Primitive `boolean` type
  44. *
  45. * @example
  46. * ```ts
  47. * type A = boolean;
  48. * ```
  49. */
  50. export declare interface BooleanTypeSchema {
  51. type: 'Boolean';
  52. }
  53. /**
  54. * Buffer type, include `ArrayBuffer` and typed arrays (e.g. `Uint8Array`, `Int32Array`...).
  55. *
  56. * @example
  57. * ```ts
  58. * type A = ArrayBuffer;
  59. * type B = Uint8Array;
  60. * ```
  61. */
  62. export declare interface BufferTypeSchema {
  63. type: 'Buffer';
  64. /**
  65. * 有该字段,代表类型为该字段对应的TypedArray,否则该字段为ArrayBuffer
  66. */
  67. arrayType?: 'Int8Array' | 'Int16Array' | 'Int32Array' | 'BigInt64Array' | 'Uint8Array' | 'Uint16Array' | 'Uint32Array' | 'BigUint64Array' | 'Float32Array' | 'Float64Array';
  68. }
  69. /**
  70. * Custom type, used for custom validate / encode / decode methods.
  71. */
  72. export declare interface CustomTypeSchema {
  73. type: 'Custom';
  74. /** Custom validate method */
  75. validate: (value: any) => {
  76. isSucc: true;
  77. errMsg?: undefined;
  78. } | {
  79. isSucc: false;
  80. errMsg: string;
  81. };
  82. /**
  83. * Custom encode method.
  84. * It is ensured that the method is called after validated successfully.
  85. */
  86. encode?: (value: any) => Uint8Array;
  87. /**
  88. * Custom decode method.
  89. * After decode, it would validate again.
  90. */
  91. decode?: (buf: Uint8Array) => any;
  92. /**
  93. * Custom encodeJSON method.
  94. * It is ensured that the method is called after validated successfully.
  95. */
  96. encodeJSON?: (value: any) => any;
  97. /**
  98. * Custom decodeJSON method.
  99. * After decode, it would validate again.
  100. */
  101. decodeJSON?: (json: any) => any;
  102. }
  103. /**
  104. * JavaScript `Date` type
  105. *
  106. * @example
  107. * ```ts
  108. * type A = Date;
  109. * ```
  110. */
  111. export declare interface DateTypeSchema {
  112. type: 'Date';
  113. }
  114. /** Encoded as 64-bit `double` type */
  115. export declare type double = number;
  116. /**
  117. * TypeScript `enum` type
  118. *
  119. * @remarks
  120. * See: {@link https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#enums}
  121. *
  122. * @example
  123. * ```ts
  124. * enum JobName {
  125. * Teacher,
  126. * Doctor,
  127. * Salesman
  128. * }
  129. *
  130. * enum Status {
  131. * Normal = 'Normal',
  132. * Expired = 'Expired'
  133. * }
  134. * ```
  135. */
  136. export declare interface EnumTypeSchema {
  137. type: 'Enum';
  138. members: {
  139. /** Encoding identifier, generated according to the order */
  140. id: number;
  141. value: string | number;
  142. }[];
  143. }
  144. /**
  145. * TypeScript indexed access type
  146. *
  147. * @remarks
  148. * `XXX['a' | 'b']` is not a `IndexedAccessType`, which should be a `{@link UnionType}`.
  149. * (Equivalent to `XXX['a'] | XXX['b']`)
  150. *
  151. * {@link https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html}
  152. *
  153. * @example
  154. * ```ts
  155. * type A = SomeInterface['XXX']
  156. * ```
  157. */
  158. export declare interface IndexedAccessTypeSchema {
  159. type: 'IndexedAccess';
  160. objectType: InterfaceTypeSchema | InterfaceReference;
  161. index: string;
  162. }
  163. /** Encoded as {@link https://developers.google.com/protocol-buffers/docs/encoding#varints | Varint} */
  164. export declare type int = number;
  165. export declare type InterfaceReference = TypeReference | PickTypeSchema | PartialTypeSchema | OverwriteTypeSchema | OmitTypeSchema;
  166. /**
  167. * TypeScript `interface`
  168. *
  169. * @remarks
  170. * See: {@link https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#interfaces}
  171. *
  172. * @example
  173. * ```ts
  174. * interface AAA {
  175. * a: string,
  176. * b?: number[],
  177. * c: {
  178. * value: boolean
  179. * }
  180. * }
  181. *
  182. * // Index Signature
  183. * interface BBB {
  184. * [key: string]: string;
  185. * }
  186. * ```
  187. */
  188. export declare interface InterfaceTypeSchema {
  189. type: 'Interface';
  190. /** Extends which interface */
  191. extends?: {
  192. id: number;
  193. type: InterfaceReference;
  194. }[];
  195. /** Self properties (not include extended) */
  196. properties?: {
  197. id: number;
  198. name: string;
  199. /** 可选字段 */
  200. optional?: boolean;
  201. type: TSBufferSchema;
  202. }[];
  203. /**
  204. * Index Signature
  205. * `undefined` represents no index signature, i.e. excess property is not allowed.
  206. * \{ [key: string]: xxx \}
  207. */
  208. indexSignature?: {
  209. keyType: 'String' | 'Number';
  210. type: TSBufferSchema;
  211. };
  212. }
  213. /**
  214. * TypeScript intersection type, like `A & B`
  215. *
  216. * @remarks
  217. * See: {@link https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types}
  218. *
  219. * @example
  220. * ```ts
  221. * type X1 = A & B;
  222. * type X2 = A & (B | C);
  223. * ```
  224. */
  225. export declare interface IntersectionTypeSchema {
  226. type: 'Intersection';
  227. members: {
  228. /** Encoding identifier, generated according to the order */
  229. id: number;
  230. type: TSBufferSchema;
  231. }[];
  232. }
  233. /**
  234. * TypeScript `keyof` feature, to get keys of an interface.
  235. *
  236. * @remarks
  237. * Type:
  238. * ```ts
  239. * interface ABC { a: string; b: string }
  240. * type Keys = keyof ABC;
  241. * ```
  242. *
  243. * Schema:
  244. * ```json
  245. * {
  246. * type: "keys",
  247. * target: {
  248. * type: "Reference",
  249. * target: "ABC"
  250. * }
  251. * }
  252. * ```
  253. */
  254. declare interface KeyofTypeSchema {
  255. type: 'Keyof';
  256. target: InterfaceReference;
  257. }
  258. /**
  259. * TypeScript literal type
  260. *
  261. * @remarks
  262. * See: {@link https://www.typescriptlang.org/docs/handbook/literal-types.html}
  263. *
  264. * Literal type is very useful to reduce encoded buffer size.
  265. * No matter how long the literal is, the encoded buffer is always less than 1 byte.
  266. *
  267. * @example
  268. * ```ts
  269. * type A = 'XXXX';
  270. *
  271. * // Always appears with UnionType, like:
  272. * type Gender = 'Male' | 'Female';
  273. * ```
  274. */
  275. export declare interface LiteralTypeSchema {
  276. type: 'Literal';
  277. literal?: string | number | boolean | null | undefined;
  278. }
  279. /**
  280. * TypeScript `NonNullable<Type>`
  281. *
  282. * @remarks
  283. * See: {@link https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype}
  284. *
  285. * @example
  286. * ```ts
  287. * type A = NonNullable<B>;
  288. * ```
  289. */
  290. export declare interface NonNullableTypeSchema {
  291. type: 'NonNullable';
  292. /** 引用目标l */
  293. target: TSBufferSchema;
  294. }
  295. /**
  296. * Primitive `number` type, with specific scalar type (like `int`, `uint`)
  297. *
  298. * @remarks
  299. * See: {@link https://www.typescriptlang.org/docs/handbook/basic-types.html#number}
  300. *
  301. * Encoding:<br/>
  302. * - {@link https://developers.google.com/protocol-buffers/docs/encoding#varints | Varint}: `int`, `uint`<br/>
  303. * - Fixed 64 bit: `double`, `bigint`, `bigint64`, `biguint64`
  304. */
  305. export declare interface NumberTypeSchema {
  306. type: 'Number';
  307. /**
  308. * @defaultValue 'double'
  309. */
  310. scalarType?: 'int' | 'uint' | 'bigint' | 'bigint64' | 'biguint64' | 'double';
  311. }
  312. /**
  313. * TypeScript `object` type
  314. *
  315. * Represents anything that is not `number`, `string`, `boolean`, `bigint`, `symbol`, `null`, or `undefined`.
  316. *
  317. * @remarks
  318. * See: {@link https://www.typescriptlang.org/docs/handbook/basic-types.html#object}
  319. *
  320. * NOTICE: Both `Object` and `Array` is valid.
  321. *
  322. * @example
  323. * ```ts
  324. * let a: object;
  325. * ```
  326. *
  327. */
  328. export declare interface ObjectTypeSchema {
  329. type: 'Object';
  330. }
  331. /**
  332. * TypeScript `Omit` type,
  333. * represents omit some properties from a interface.
  334. *
  335. * @remarks
  336. * See: {@link https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys}
  337. *
  338. * @example
  339. * ```ts
  340. * interface AAA {
  341. * a: string,
  342. * b: string,
  343. * c: string
  344. * }
  345. *
  346. * // Equivalent to `{ c: string }`
  347. * type BBB = Omit<AAA, 'a' | 'b'>;
  348. * ```
  349. */
  350. export declare interface OmitTypeSchema {
  351. type: 'Omit';
  352. target: InterfaceTypeSchema | InterfaceReference | UnionTypeSchema | IntersectionTypeSchema;
  353. keys: string[];
  354. }
  355. /**
  356. * Overwrite some properties from a interface
  357. */
  358. export declare type Overwrite<T, U> = T extends unknown ? Pick<T, Exclude<keyof T, keyof U>> & U : never;
  359. /**
  360. * TSBuffer utility type, which represents overwrite some properties from a interface.
  361. *
  362. * @example
  363. * ```ts
  364. * import { Overwrite } from 'tsbuffer-schema';
  365. *
  366. * interface AAA {
  367. * a: string,
  368. * b: string
  369. * }
  370. *
  371. * // Equivalent to `{ a: string, b: number, c: number }`
  372. * type BBB = Overwrite<AAA, {
  373. * b: number,
  374. * c: number
  375. * }>
  376. * ```
  377. */
  378. export declare interface OverwriteTypeSchema {
  379. type: 'Overwrite';
  380. target: InterfaceTypeSchema | InterfaceReference;
  381. overwrite: InterfaceTypeSchema | InterfaceReference;
  382. }
  383. /**
  384. * TypeScript `Partial<Type>`
  385. *
  386. * @remarks
  387. * See: {@link https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype}
  388. */
  389. export declare interface PartialTypeSchema {
  390. type: 'Partial';
  391. target: InterfaceTypeSchema | InterfaceReference | UnionTypeSchema | IntersectionTypeSchema;
  392. }
  393. /**
  394. * TypeScript `Pick<Type>`
  395. * @remarks
  396. * See: {@link https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys}
  397. */
  398. export declare interface PickTypeSchema {
  399. type: 'Pick';
  400. target: InterfaceTypeSchema | InterfaceReference | UnionTypeSchema | IntersectionTypeSchema;
  401. keys: string[];
  402. }
  403. /**
  404. * Type Reference, infers that this type should be equivalent to target type.
  405. *
  406. * @remarks
  407. * If target type is in a namespace, the schema should be like below.
  408. *
  409. * Type:
  410. * ```ts
  411. * import { SomeNS } from './some-ns';
  412. * type Reference = SomeNS.Target;
  413. * ```
  414. *
  415. * Schema:
  416. * ```json
  417. * {
  418. * type: "Reference",
  419. * target: "./some-ns/SomeNS.Target"
  420. * }
  421. * ```
  422. *
  423. * When parsing a type reference, if `target` includes point, namespace would be parsed by `target.split('.')`.
  424. * If `target` doesn't include point, it would be treated as a type name.
  425. */
  426. export declare interface ReferenceTypeSchema {
  427. type: 'Reference';
  428. /** SchemaID of reference target */
  429. target: string;
  430. }
  431. /**
  432. * Enum for every possible `TSBufferSchema['type']`
  433. */
  434. export declare class SchemaType {
  435. static Boolean: "Boolean";
  436. static Number: "Number";
  437. static String: "String";
  438. static Array: "Array";
  439. static Tuple: "Tuple";
  440. static Enum: "Enum";
  441. static Any: "Any";
  442. static Literal: "Literal";
  443. static Object: "Object";
  444. static Interface: "Interface";
  445. static Buffer: "Buffer";
  446. static IndexedAccess: "IndexedAccess";
  447. static Reference: "Reference";
  448. static Keyof: "Keyof";
  449. static Union: "Union";
  450. static Intersection: "Intersection";
  451. static NonNullable: "NonNullable";
  452. static Date: "Date";
  453. static Pick: "Pick";
  454. static Partial: "Partial";
  455. static Omit: "Omit";
  456. static Overwrite: "Overwrite";
  457. static Custom: "Custom";
  458. }
  459. /**
  460. * Primitive `string` type
  461. */
  462. export declare interface StringTypeSchema {
  463. type: 'String';
  464. }
  465. /**
  466. * Collection of a group of `TSBufferSchema`
  467. */
  468. export declare interface TSBufferProto {
  469. /**
  470. * [schemaId: string]: `{relativePath}/{namespace}/{typeName}`.
  471. * `path` is relative path to `baseDir`, without extension name.
  472. *
  473. * @example
  474. * a/b/c/index.ts:
  475. * ```ts
  476. * export interface Test {
  477. * a: string;
  478. * }
  479. * ```
  480. * schemaId for `Test` is `a/b/c/index/Test`
  481. *
  482. * a/b/c/index.ts (with namespace)
  483. * ```ts
  484. * export namespace NS {
  485. * export interface Test {
  486. * value: string;
  487. * }
  488. * }
  489. * ```
  490. * schemaId for `NS.Test` is `a/b/c/index/NS/Test`
  491. */
  492. [schemaId: string]: TSBufferSchema;
  493. }
  494. /**
  495. * Schema for TypeScript Types
  496. */
  497. export declare type TSBufferSchema = (BooleanTypeSchema | NumberTypeSchema | StringTypeSchema | ArrayTypeSchema | TupleTypeSchema | EnumTypeSchema | AnyTypeSchema | LiteralTypeSchema | ObjectTypeSchema | InterfaceTypeSchema | BufferTypeSchema | IndexedAccessTypeSchema | ReferenceTypeSchema | KeyofTypeSchema | UnionTypeSchema | IntersectionTypeSchema | PickTypeSchema | PartialTypeSchema | OmitTypeSchema | OverwriteTypeSchema | NonNullableTypeSchema | DateTypeSchema | CustomTypeSchema) & {
  498. comment?: string;
  499. };
  500. /**
  501. * TypeScript `Tuple` type
  502. *
  503. * @remarks
  504. * It has less encoded size than `Array`.
  505. *
  506. * See: {@link https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types}
  507. */
  508. export declare interface TupleTypeSchema {
  509. type: 'Tuple';
  510. elementTypes: TSBufferSchema[];
  511. optionalStartIndex?: number;
  512. }
  513. export declare type TypeReference = ReferenceTypeSchema | IndexedAccessTypeSchema | KeyofTypeSchema;
  514. /** Encoded as {@link https://developers.google.com/protocol-buffers/docs/encoding#varints | Varint} */
  515. export declare type uint = number;
  516. /**
  517. * TypeScript Union Types, like `A | B`
  518. *
  519. * @remarks
  520. * See: {@link https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types}
  521. *
  522. * @example
  523. * ```ts
  524. * type X1 = A | B;
  525. * type X2 = A | null | undefined;
  526. * type Gender = 'Male' | 'Female';
  527. * ```
  528. */
  529. export declare interface UnionTypeSchema {
  530. type: 'Union';
  531. members: {
  532. id: number;
  533. type: TSBufferSchema;
  534. }[];
  535. }
  536. export { }