@userscripters/stackexchange-api-types
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -1,3 +0,9 @@ | ||
import type { NodeFactory, TypeNode } from "typescript"; | ||
import type { | ||
Identifier, | ||
KeywordTypeSyntaxKind, | ||
NodeFactory, | ||
TypeNode, | ||
} from "typescript"; | ||
import ts from "typescript"; | ||
import { parseKeyword } from "./parsers.js"; | ||
@@ -23,1 +29,33 @@ /** | ||
); | ||
export type TypeParameterOptions = { | ||
constraint?: KeywordTypeSyntaxKind; | ||
defaults?: KeywordTypeSyntaxKind; | ||
}; | ||
/** | ||
* @summary creates a type parameter for use with generics | ||
* @param f compiler factory to use | ||
* @param name property name | ||
* @param options configuration | ||
*/ | ||
export const createTypeParameter = ( | ||
f: NodeFactory, | ||
name: string | Identifier, | ||
{ constraint, defaults }: TypeParameterOptions | ||
): ts.TypeParameterDeclaration => { | ||
return f.createTypeParameterDeclaration( | ||
typeof name === "string" ? f.createIdentifier(name) : name, | ||
constraint && f.createKeywordTypeNode(constraint), | ||
defaults && f.createKeywordTypeNode(defaults) | ||
); | ||
}; | ||
/** | ||
* @summary creates an array of keword type nodes | ||
* @param f compiler factory to use | ||
* @param name identifier name for array values | ||
*/ | ||
export const createKeywordArray = (f: NodeFactory, name: string) => { | ||
return f.createArrayTypeNode(parseKeyword(f, name)); | ||
}; |
import got from "got"; | ||
import type { Node, __String } from "typescript"; | ||
import type ts from "typescript"; | ||
import { __String } from "typescript"; | ||
import { URL } from "url"; | ||
import { InterfaceOptions, parseInterface } from "./parsers.js"; | ||
import { printNodesToFile } from "./printer.js"; | ||
import { getDocument, partition, sleep } from "./utils.js"; | ||
import { generateResponseWrapper } from "./wrapper.js"; | ||
@@ -73,17 +76,5 @@ const DOCS_BASE = "https://api.stackexchange.com"; | ||
const printer = ts.createPrinter({ | ||
newLine: ts.NewLineKind.LineFeed, | ||
}); | ||
const typesFile = ts.createSourceFile( | ||
"types", | ||
"", | ||
ts.ScriptTarget.Latest, | ||
false, | ||
ts.ScriptKind.TS | ||
); | ||
const nsName = "StackExchangeAPI"; | ||
const nodes: Map<__String, Node> = new Map(); | ||
const nodes: Map<__String, ts.Node> = new Map(); | ||
@@ -96,3 +87,5 @@ const unionRegex = | ||
overrides: { | ||
event_id: ts.SyntaxKind.NumberKeyword, | ||
event_id: factory.createKeywordTypeNode( | ||
ts.SyntaxKind.NumberKeyword | ||
), | ||
}, | ||
@@ -124,3 +117,3 @@ }; | ||
const unique: Node[] = []; | ||
const unique: ts.Node[] = []; | ||
for (const [, node] of nodes) { | ||
@@ -132,8 +125,15 @@ unique.push(node, factory.createIdentifier("\n")); | ||
const list = factory.createNodeArray(unique); | ||
const content = printer.printList(ts.ListFormat.MultiLine, list, typesFile); | ||
await printNodesToFile(ts, unique, `${TYPES_PATH}/types.d.ts`); | ||
const { writeFile } = await import("fs/promises"); | ||
await writeFile(`${TYPES_PATH}/types.d.ts`, content); | ||
await generateResponseWrapper( | ||
factory, | ||
DOCS_BASE, | ||
"/docs/wrapper", | ||
`${TYPES_PATH}/wrapper.d.ts`, | ||
typeNameSel, | ||
typeFieldsSel, | ||
typeReqSel, | ||
unionRegex, | ||
nsName | ||
); | ||
} |
@@ -1,2 +0,9 @@ | ||
import type { KeywordTypeSyntaxKind, Modifier, NodeFactory } from "typescript"; | ||
import type { | ||
ArrayTypeNode, | ||
KeywordTypeNode, | ||
KeywordTypeSyntaxKind, | ||
Modifier, | ||
NodeFactory, | ||
TypeParameterDeclaration, | ||
} from "typescript"; | ||
import ts from "typescript"; | ||
@@ -84,3 +91,4 @@ import { createProperty } from "./factories.js"; | ||
exported?: boolean; | ||
overrides?: Record<string, KeywordTypeSyntaxKind>; | ||
overrides?: Record<string, KeywordTypeNode | ArrayTypeNode>; | ||
parameters?: TypeParameterDeclaration[]; | ||
}; | ||
@@ -104,3 +112,3 @@ | ||
unionRegex: RegExp, | ||
{ exported = false, overrides = {} }: InterfaceOptions = {} | ||
{ exported = false, overrides = {}, parameters = [] }: InterfaceOptions = {} | ||
) => { | ||
@@ -128,9 +136,3 @@ const { textContent } = document.querySelector(nameSelector) || {}; | ||
const override = overrides[fieldName]; | ||
return override | ||
? createProperty( | ||
factory, | ||
fieldName, | ||
factory.createKeywordTypeNode(override) | ||
) | ||
: field; | ||
return override ? createProperty(factory, fieldName, override) : field; | ||
}); | ||
@@ -146,6 +148,6 @@ | ||
name, | ||
parameters, | ||
undefined, | ||
undefined, | ||
overridden | ||
); | ||
}; |
@@ -28,4 +28,8 @@ import got from "got"; | ||
/** | ||
* @summary properly capitalizes the type name and joins words on underscore or space | ||
* @param name non-normalized type name (i.e. just parsed from docs) | ||
*/ | ||
export const normalizeTypeName = (name: string) => | ||
name.split("_").map(capitalize).join(""); | ||
name.split(/[_ ]/).map(capitalize).join(""); | ||
@@ -32,0 +36,0 @@ /** |
{ | ||
"name": "@userscripters/stackexchange-api-types", | ||
"description": "Stack Exchange API types generator", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"author": { | ||
@@ -38,6 +38,6 @@ "name": "Oleg Valter", | ||
"got": "^11.8.2", | ||
"jsdom": "^17.0.0", | ||
"ts-node": "^10.2.1", | ||
"tsd": "^0.17.0", | ||
"typescript": "^4.4.3" | ||
"jsdom": "^18.0.0", | ||
"ts-node": "^10.4.0", | ||
"tsd": "^0.18.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
@@ -44,0 +44,0 @@ "type": "module", |
@@ -9,3 +9,3 @@ | ||
| License | [GPL-3.0-or-later](https://spdx.org/licenses/GPL-3.0-or-later) | | ||
| Version | 2.0.0 | | ||
| Version | 3.0.0 | | ||
@@ -12,0 +12,0 @@ # Support |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40139
20
1040