const {query} = require("../client") const {Tag} = require("./Tag") const {Chapter} = require("./Chapter") class Story { /** * Construct a story object. You should use the API instead of calling this. * * @param {string} id * @param {string} name * @param {string} author * @param {"Info"|"News"|"Document"|"Background"|"Story"} category Story's category * @param {string} createdDate * @param {string} updatedDate * @param {string} fictionalDate * @param {{kind:string, name:string}[]} tags * @param {Chapter[]} chapters */ constructor(id, name, author, category, createdDate, updatedDate, fictionalDate, tags, chapters) { this.id = id this.name = name this.author = author this.category = category this.createdDate = new Date(createdDate) this.updatedDate = new Date(updatedDate) this.fictionalDate = new Date(fictionalDate) this.tags = tags.map(dt => new Tag(dt.kind, dt.name)) this.chapters = chapters != null ? chapters.map(c => new Chapter(c.id, c.title, c.author, c.source, c.createdDate, c.fictionalDate, c.editedDate)) : null } } class StoryCategory { constructor(name, description) { this.name = name this.description = description } } const data = { categories: null } /** * storyApi contains the API queries */ const storyApi = { /** * Call `stories(filter)` query * * @param {{author:string, category:string, tags:Tag[]|Tag, unlisted: boolean, open: boolean, earliestFictionalDate:Date|string, latestFictionalDate:Date|string, limit:number}} filter * @returns {Promise} */ list(filter = {}) { if (filter.earliestFictionalDate != null && typeof(filter.earliestFictionalDate) !== "string") { filter.earliestFictionalDate = filter.earliestFictionalDate.toISOString() } if (filter.latestFictionalDate != null && typeof(filter.latestFictionalDate) !== "string") { filter.latestFictionalDate = filter.latestFictionalDate.toISOString() } return query(` query ListStories($filter: StoriesFilter) { stories(filter: $filter) { id name author category tags { kind name } createdDate fictionalDate updatedDate } } `, {filter}).then(({stories}) => { return stories.map(d => new Story(d.id, d.name, d.author, d.category, d.createdDate, d.updatedDate, d.fictionalDate, d.tags)) }) }, /** * @param {string} id * @returns {Promise} */ find(id) { return query(` query FindStory($id: String!) { story(id: $id) { id name author category tags { kind name } createdDate fictionalDate updatedDate chapters { id title author source createdDate fictionalDate editedDate } } } `, {id}).then(({story}) => { return new Story( story.id, story.name, story.author, story.category, story.createdDate, story.updatedDate, story.fictionalDate, story.tags, story.chapters ) }) }, /** * Call `__type(name: "StoryCategory")` query and extracts the catogires from it * * @returns {Promise} */ categories() { if (data.categories !== null) { return data.categories } return query(` query ListStoryCategories { categoryType: __type(name: "StoryCategory") { enumValues { name description } } } `, {}).then(({categoryType}) => { data.categories = categoryType.enumValues.map(d => new StoryCategory(d.name, d.description)) return data.categories }) }, } module.exports = {storyApi, Story}