Skip to content

Instantly share code, notes, and snippets.

@andyg0808
Last active June 15, 2022 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyg0808/a7b7197dd917a2e87362781924241cbf to your computer and use it in GitHub Desktop.
Save andyg0808/a7b7197dd917a2e87362781924241cbf to your computer and use it in GitHub Desktop.
iFixit API TypeScript types: Guide, generated by https://app.quicktype.io/
export interface Guide {
conclusion_raw: string;
conclusion_rendered: string;
difficulty: string;
documents: any[];
flags: Flag[];
guideid: number;
image: Image;
introduction_raw: string;
introduction_rendered: string;
featured_document_embed_url: string;
featured_document_thumbnail_url: string;
locale: string;
parts: any[];
prerequisites: any[];
steps: Step[];
subject: string;
summary: string;
time_required: string;
time_required_min: number;
time_required_max: number;
title: string;
tools: Tool[];
type: string;
revisionid: number;
created_date: number;
published_date: number;
modified_date: number;
prereq_modified_date: number;
public: boolean;
comments: any[];
category: string;
url: string;
patrol_threshold: number;
can_edit: boolean;
favorited: boolean;
author: Author;
langid: string;
featured_documentid: null;
intro_video_url: null;
intro_video: null;
completed: boolean;
}
export interface Author {
userid: number;
username: string;
unique_username: null;
join_date: number | null;
image: Image;
reputation: number;
url: string;
teams: any[];
}
export interface Image {
id: number;
guid: string;
original: string;
mini: string;
thumbnail: string;
"140x105"?: string;
"200x150"?: string;
standard?: string;
"440x330"?: string;
medium: string;
large?: string;
}
export interface Flag {
title: string;
flagid: string;
text: string;
}
export interface Step {
title: string;
lines: Line[];
guideid: number;
stepid: number;
orderby: number;
revisionid: number;
media: Media;
comments: Comment[];
}
export interface Comment {
commentid: number;
locale: string;
context: string;
contextid: number;
parentid: null;
author: Author;
title: string;
text_raw: string;
text_rendered: string;
rating: number;
date: number;
modified_date: number;
status: string;
replies: any[];
}
export interface Line {
text_raw: string;
bullet: Bullet;
level: number;
lineid: null;
text_rendered: string;
}
export enum Bullet {
Black = "black",
}
export interface Media {
type: string;
data: Image[];
}
export interface Tool {
type: string;
quantity: number;
text: string;
notes: null;
url: string;
thumbnail: string;
}
// To parse this data:
//
// const Convert = require("./file");
//
// const guide = Convert.toGuide(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.
// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
function toGuide(json) {
return cast(JSON.parse(json), r("Guide"));
}
function guideToJson(value) {
return JSON.stringify(uncast(value, r("Guide")), null, 2);
}
function invalidValue(typ, val, key = '') {
if (key) {
throw Error(`Invalid value for key "${key}". Expected type ${JSON.stringify(typ)} but got ${JSON.stringify(val)}`);
}
throw Error(`Invalid value ${JSON.stringify(val)} for type ${JSON.stringify(typ)}`, );
}
function jsonToJSProps(typ) {
if (typ.jsonToJS === undefined) {
const map = {};
typ.props.forEach((p) => map[p.json] = { key: p.js, typ: p.typ });
typ.jsonToJS = map;
}
return typ.jsonToJS;
}
function jsToJSONProps(typ) {
if (typ.jsToJSON === undefined) {
const map = {};
typ.props.forEach((p) => map[p.js] = { key: p.json, typ: p.typ });
typ.jsToJSON = map;
}
return typ.jsToJSON;
}
function transform(val, typ, getProps, key = '') {
function transformPrimitive(typ, val) {
if (typeof typ === typeof val) return val;
return invalidValue(typ, val, key);
}
function transformUnion(typs, val) {
// val must validate against one typ in typs
const l = typs.length;
for (let i = 0; i < l; i++) {
const typ = typs[i];
try {
return transform(val, typ, getProps);
} catch (_) {}
}
return invalidValue(typs, val);
}
function transformEnum(cases, val) {
if (cases.indexOf(val) !== -1) return val;
return invalidValue(cases, val);
}
function transformArray(typ, val) {
// val must be an array with no invalid elements
if (!Array.isArray(val)) return invalidValue("array", val);
return val.map(el => transform(el, typ, getProps));
}
function transformDate(val) {
if (val === null) {
return null;
}
const d = new Date(val);
if (isNaN(d.valueOf())) {
return invalidValue("Date", val);
}
return d;
}
function transformObject(props, additional, val) {
if (val === null || typeof val !== "object" || Array.isArray(val)) {
return invalidValue("object", val);
}
const result = {};
Object.getOwnPropertyNames(props).forEach(key => {
const prop = props[key];
const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
result[prop.key] = transform(v, prop.typ, getProps, prop.key);
});
Object.getOwnPropertyNames(val).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(props, key)) {
result[key] = transform(val[key], additional, getProps, key);
}
});
return result;
}
if (typ === "any") return val;
if (typ === null) {
if (val === null) return val;
return invalidValue(typ, val);
}
if (typ === false) return invalidValue(typ, val);
while (typeof typ === "object" && typ.ref !== undefined) {
typ = typeMap[typ.ref];
}
if (Array.isArray(typ)) return transformEnum(typ, val);
if (typeof typ === "object") {
return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
: typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
: typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
: invalidValue(typ, val);
}
// Numbers can be parsed by Date but shouldn't be.
if (typ === Date && typeof val !== "number") return transformDate(val);
return transformPrimitive(typ, val);
}
function cast(val, typ) {
return transform(val, typ, jsonToJSProps);
}
function uncast(val, typ) {
return transform(val, typ, jsToJSONProps);
}
function a(typ) {
return { arrayItems: typ };
}
function u(...typs) {
return { unionMembers: typs };
}
function o(props, additional) {
return { props, additional };
}
function m(additional) {
return { props: [], additional };
}
function r(name) {
return { ref: name };
}
const typeMap = {
"Guide": o([
{ json: "conclusion_raw", js: "conclusion_raw", typ: "" },
{ json: "conclusion_rendered", js: "conclusion_rendered", typ: "" },
{ json: "difficulty", js: "difficulty", typ: "" },
{ json: "documents", js: "documents", typ: a("any") },
{ json: "flags", js: "flags", typ: a(r("Flag")) },
{ json: "guideid", js: "guideid", typ: 0 },
{ json: "image", js: "image", typ: r("Image") },
{ json: "introduction_raw", js: "introduction_raw", typ: "" },
{ json: "introduction_rendered", js: "introduction_rendered", typ: "" },
{ json: "featured_document_embed_url", js: "featured_document_embed_url", typ: "" },
{ json: "featured_document_thumbnail_url", js: "featured_document_thumbnail_url", typ: "" },
{ json: "locale", js: "locale", typ: "" },
{ json: "parts", js: "parts", typ: a("any") },
{ json: "prerequisites", js: "prerequisites", typ: a("any") },
{ json: "steps", js: "steps", typ: a(r("Step")) },
{ json: "subject", js: "subject", typ: "" },
{ json: "summary", js: "summary", typ: "" },
{ json: "time_required", js: "time_required", typ: "" },
{ json: "time_required_min", js: "time_required_min", typ: 0 },
{ json: "time_required_max", js: "time_required_max", typ: 0 },
{ json: "title", js: "title", typ: "" },
{ json: "tools", js: "tools", typ: a(r("Tool")) },
{ json: "type", js: "type", typ: "" },
{ json: "revisionid", js: "revisionid", typ: 0 },
{ json: "created_date", js: "created_date", typ: 0 },
{ json: "published_date", js: "published_date", typ: 0 },
{ json: "modified_date", js: "modified_date", typ: 0 },
{ json: "prereq_modified_date", js: "prereq_modified_date", typ: 0 },
{ json: "public", js: "public", typ: true },
{ json: "comments", js: "comments", typ: a("any") },
{ json: "category", js: "category", typ: "" },
{ json: "url", js: "url", typ: "" },
{ json: "patrol_threshold", js: "patrol_threshold", typ: 0 },
{ json: "can_edit", js: "can_edit", typ: true },
{ json: "favorited", js: "favorited", typ: true },
{ json: "author", js: "author", typ: r("Author") },
{ json: "langid", js: "langid", typ: "" },
{ json: "featured_documentid", js: "featured_documentid", typ: null },
{ json: "intro_video_url", js: "intro_video_url", typ: null },
{ json: "intro_video", js: "intro_video", typ: null },
{ json: "completed", js: "completed", typ: true },
], false),
"Author": o([
{ json: "userid", js: "userid", typ: 0 },
{ json: "username", js: "username", typ: "" },
{ json: "unique_username", js: "unique_username", typ: null },
{ json: "join_date", js: "join_date", typ: u(0, null) },
{ json: "image", js: "image", typ: r("Image") },
{ json: "reputation", js: "reputation", typ: 0 },
{ json: "url", js: "url", typ: "" },
{ json: "teams", js: "teams", typ: a("any") },
], false),
"Image": o([
{ json: "id", js: "id", typ: 0 },
{ json: "guid", js: "guid", typ: "" },
{ json: "original", js: "original", typ: "" },
{ json: "mini", js: "mini", typ: "" },
{ json: "thumbnail", js: "thumbnail", typ: "" },
{ json: "140x105", js: "140x105", typ: u(undefined, "") },
{ json: "200x150", js: "200x150", typ: u(undefined, "") },
{ json: "standard", js: "standard", typ: u(undefined, "") },
{ json: "440x330", js: "440x330", typ: u(undefined, "") },
{ json: "medium", js: "medium", typ: "" },
{ json: "large", js: "large", typ: u(undefined, "") },
], false),
"Flag": o([
{ json: "title", js: "title", typ: "" },
{ json: "flagid", js: "flagid", typ: "" },
{ json: "text", js: "text", typ: "" },
], false),
"Step": o([
{ json: "title", js: "title", typ: "" },
{ json: "lines", js: "lines", typ: a(r("Line")) },
{ json: "guideid", js: "guideid", typ: 0 },
{ json: "stepid", js: "stepid", typ: 0 },
{ json: "orderby", js: "orderby", typ: 0 },
{ json: "revisionid", js: "revisionid", typ: 0 },
{ json: "media", js: "media", typ: r("Media") },
{ json: "comments", js: "comments", typ: a(r("Comment")) },
], false),
"Comment": o([
{ json: "commentid", js: "commentid", typ: 0 },
{ json: "locale", js: "locale", typ: "" },
{ json: "context", js: "context", typ: "" },
{ json: "contextid", js: "contextid", typ: 0 },
{ json: "parentid", js: "parentid", typ: null },
{ json: "author", js: "author", typ: r("Author") },
{ json: "title", js: "title", typ: "" },
{ json: "text_raw", js: "text_raw", typ: "" },
{ json: "text_rendered", js: "text_rendered", typ: "" },
{ json: "rating", js: "rating", typ: 0 },
{ json: "date", js: "date", typ: 0 },
{ json: "modified_date", js: "modified_date", typ: 0 },
{ json: "status", js: "status", typ: "" },
{ json: "replies", js: "replies", typ: a("any") },
], false),
"Line": o([
{ json: "text_raw", js: "text_raw", typ: "" },
{ json: "bullet", js: "bullet", typ: r("Bullet") },
{ json: "level", js: "level", typ: 0 },
{ json: "lineid", js: "lineid", typ: null },
{ json: "text_rendered", js: "text_rendered", typ: "" },
], false),
"Media": o([
{ json: "type", js: "type", typ: "" },
{ json: "data", js: "data", typ: a(r("Image")) },
], false),
"Tool": o([
{ json: "type", js: "type", typ: "" },
{ json: "quantity", js: "quantity", typ: 0 },
{ json: "text", js: "text", typ: "" },
{ json: "notes", js: "notes", typ: null },
{ json: "url", js: "url", typ: "" },
{ json: "thumbnail", js: "thumbnail", typ: "" },
], false),
"Bullet": [
"black",
],
};
module.exports = {
"guideToJson": guideToJson,
"toGuide": toGuide,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment