From 56220377198d897f7a05fd84ed86899199568f0d Mon Sep 17 00:00:00 2001 From: bee Date: Tue, 1 Mar 2022 19:46:17 -0800 Subject: [PATCH] temporarily add SAPI --- .gitignore | 1 - SimplyAPI/examples/groups.js | 61 ++++++++++++ SimplyAPI/examples/members.js | 58 ++++++++++++ SimplyAPI/examples/other.js | 26 ++++++ SimplyAPI/index.js | 6 ++ SimplyAPI/lib/Schemas.js | 100 ++++++++++++++++++++ SimplyAPI/lib/SimplyAPI.js | 170 ++++++++++++++++++++++++++++++++++ SimplyAPI/lib/Validate.js | 11 +++ SimplyAPI/package.json | 20 ++++ 9 files changed, 452 insertions(+), 1 deletion(-) create mode 100644 SimplyAPI/examples/groups.js create mode 100644 SimplyAPI/examples/members.js create mode 100644 SimplyAPI/examples/other.js create mode 100644 SimplyAPI/index.js create mode 100644 SimplyAPI/lib/Schemas.js create mode 100644 SimplyAPI/lib/SimplyAPI.js create mode 100644 SimplyAPI/lib/Validate.js create mode 100644 SimplyAPI/package.json diff --git a/.gitignore b/.gitignore index 31e6671..0befe13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -/SimplyAPI /test.js /node_modules /.vscode diff --git a/SimplyAPI/examples/groups.js b/SimplyAPI/examples/groups.js new file mode 100644 index 0000000..841c575 --- /dev/null +++ b/SimplyAPI/examples/groups.js @@ -0,0 +1,61 @@ +const config = require('./config.json') +const SAPI = require('./SimplyAPI.js') +const SimplyAPI = new SAPI(config) + +let group = { + parent: "root", + color: "", + private: true, + preventTrusted: false, + name: "123", + desc: "test group", + emoji: "", + members: [] +} + +main = async () => { + getGroups() + findGroup("123") + createTestGroup(group) + deleteTestGroup("123") +} + +getGroups = async () => { + SimplyAPI.getGroups() + .then((response) => { + console.log(response.data) + }) + .catch(err => console.error(err)) +} + +findGroup = async (what) => { + SimplyAPI.findGroup(what, (group) => { + if (group) { + console.log(group) + } + }) +} + +createTestGroup = async (data) => { + SimplyAPI.createGroup(data) + .then((response) => { + console.log(response.data) + }) + .catch(err => console.error(err)) +} + +deleteTestGroup = async (what) => { + await SimplyAPI.findGroup(what, async (group) => { + if (group) { + await SimplyAPI.deleteGroup(group.id) + .then(async (res) => { + if (res.status == 200) { + console.log(`group deleted: ${group.content.name}.`,) + } + }) + .catch(err => console.error(err)) + } + }) +} + +main() \ No newline at end of file diff --git a/SimplyAPI/examples/members.js b/SimplyAPI/examples/members.js new file mode 100644 index 0000000..877d88e --- /dev/null +++ b/SimplyAPI/examples/members.js @@ -0,0 +1,58 @@ +const config = require('./config.json') +const SAPI = require('./lib/SimplyAPI.js') +const SimplyAPI = new SAPI(config) + +let member = { + name: "Test", + desc: "a test member", + pronouns: "It/Its", + pkId: "", + color: "", + avatarUuid: "", + avatarUrl: "", + private: false, + preventTrusted: false, + preventFrontNotifs: false, + info: { + "Age": "19", + "Likes": "bread" + } +} + +main = async () => { + findMember("Test") + createTestMember(member) + deleteTestMember("Test") +} + +findMember = async (who) => { + SimplyAPI.findMemberCallback(who, (member) => { + if (member) { + console.log(member) + } + }) +} + +createTestMember = async (data) => { + SimplyAPI.createMember(data) + .then((response) => { + console.log(response.data) + }) + .catch(err => console.error(err)) +} + +deleteTestMember = async (who) => { + await SimplyAPI.findMember(who, async (member) => { + if (member) { + await SimplyAPI.deleteMember(member.id) + .then((res) => { + if (res.status == 200) { + console.log(`member deleted: ${res.data.content.name}.`) + } + }) + .catch(err => console.error(err)) + } + }) +} + +main() \ No newline at end of file diff --git a/SimplyAPI/examples/other.js b/SimplyAPI/examples/other.js new file mode 100644 index 0000000..5576231 --- /dev/null +++ b/SimplyAPI/examples/other.js @@ -0,0 +1,26 @@ +const config = require('./config.json') +const SAPI = require('./SimplyAPI.js') +const SimplyAPI = new SAPI(config) + +main = async () => { + getSystem() + getCurrentFronters() +} + +getSystem = async () => { + SimplyAPI.getSystem() + .then((response) => { + console.log(response.data) + }) + .catch(err => console.error(err)) +} + +getCurrentFronters = async () => { + SimplyAPI.getFronters() + .then((response) => { + console.log(response) + }) + .catch(err => console.error(err)) +} + +main() \ No newline at end of file diff --git a/SimplyAPI/index.js b/SimplyAPI/index.js new file mode 100644 index 0000000..a7c81da --- /dev/null +++ b/SimplyAPI/index.js @@ -0,0 +1,6 @@ +const SimplyAPI = require('./lib/SimplyAPI') + +SimplyAPI.Validate = require('./lib/Validate') +SimplyAPI.Schemas = require('./lib/Schemas') + +module.exports = SimplyAPI; \ No newline at end of file diff --git a/SimplyAPI/lib/Schemas.js b/SimplyAPI/lib/Schemas.js new file mode 100644 index 0000000..c247561 --- /dev/null +++ b/SimplyAPI/lib/Schemas.js @@ -0,0 +1,100 @@ +const memberSchema = { + type: "object", + properties: { + name: { type: "string" }, + desc: { type: "string" }, + pronouns: { type: "string" }, + pkId: { type: "string" }, + color: { type: "string" }, + avatarUuid: { type: "string" }, + avatarUrl: { type: "string" }, + private: { type: "boolean" }, + preventTrusted: { type: "boolean" }, + preventFrontNotifs: { type: "boolean" }, + info: { + type: "object", + properties: { + "*": { type: "string" } + } + } + }, + nullable: false, + additionalProperties: false, +}; + +const groupSchema = { + type: "object", + properties: { + parent: { type: "string" }, + color: { type: "string" }, + private: { type: "boolean" }, + preventTrusted: { type: "boolean" }, + name: { type: "string" }, + desc: { type: "string" }, + emoji: { type: "string" }, + members: { type: "array", items: { type: "string" } }, + }, + nullable: false, + additionalProperties: false, + dependencies: { + private: { required: ["preventTrusted"] }, + preventTrusted: { required: ["private"] }, + } +}; + +const customFrontSchema = { + type: "object", + properties: { + name: { type: "string" }, + desc: { type: "string" }, + avatarUrl: { type: "string" }, + avatarUuid: { type: "string" }, + color: { type: "string" }, + preventTrusted: { type: "boolean" }, + private: { type: "boolean" }, + }, + nullable: false, + additionalProperties: false, +} + +const commentSchema = { + type: "object", + properties: { + time: { type: "number" }, + text: { type: "string" }, + documentId: { type: "string" }, + collection: { type: "string" } + }, + nullable: false, + additionalProperties: false, + required: ["time", "text", "documentId", "collection"] +} + +const commentPatchSchema = { + type: "object", + properties: { + text: { type: "string" }, + }, + nullable: false, + additionalProperties: false, + required: ["text"] +} + +const automatedTimerSchema = { + type: "object", + properties: { + name: { type: "string" }, + message: { type: "string" }, + action: { type: "number" }, + delayInHours: { type: "number" }, + type: { type: "number" }, + }, + nullable: false, + additionalProperties: false, +}; + + +module.exports = { + memberSchema, + groupSchema +} \ No newline at end of file diff --git a/SimplyAPI/lib/SimplyAPI.js b/SimplyAPI/lib/SimplyAPI.js new file mode 100644 index 0000000..2e943bc --- /dev/null +++ b/SimplyAPI/lib/SimplyAPI.js @@ -0,0 +1,170 @@ +const { resolveRef } = require('ajv/dist/compile') +const axios = require('axios') +const schemas = require('./Schemas') +const validate = require('./Validate') +/** + * @param {Object} config + */ +class SimplyAPI { + constructor(config) { + this.url = config.url_override || 'https://devapi.apparyllis.com' + this.userId = config.userId + this.system = config.userId + this.token = config.token + this.header = { + 'Content-Type': 'application/json', + 'Authorization': this.token + } + } + + getSystem = async () => { + let system = await axios.get(`${this.url}/v1/members/${this.system}`, { + headers: this.header + }) + return system.data + //.then((response) => response) + //.catch(err => console.error(err.toJSON().message)); + } + + getGroups = async () => { + return axios.get(`${this.url}/v1/groups/${this.system}`, { + headers: this.header + }) + .then((response) => response) + .catch(err => console.error(err.toJSON().message)); + } + + /** + * @param {string} group + * @param {function} callback + */ + findGroup = async (group, callback) => { + await this.getGroups() + .then((groups) => { + for (let i in groups.data) { + if (groups.data[i].content.name.includes(group)) { + callback(groups.data[i]) + return + } + } + }) + + } + + createGroup = async (group) => { + let valid = await validate.validateSchema(schemas.groupSchema, group) + + if (valid) { + return axios.post(`${this.url}/v1/group/`, JSON.stringify(group), { + headers: this.header, + }) + .then((response) => response) + .catch(err => console.error(err.toJSON().message)); + } else { + let response = {} + response.data = {status: 'error', message: 'Invalid group schema'} + return response + } + } + + deleteGroup = async (group) => { + return await axios.delete(`${this.url}/v1/group/${group}`, { + headers: this.header, + }) + .then((response) => response) + .catch(err => console.error(err.toJSON().message)); + } + + /** + * @param {string} id + */ + findMemberById = async (id) => { + let found = false + let system = await this.getSystem() + return new Promise(async (resolve) => { + await asyncForEach(system, async (m) => { + if (m.id == id) { + found = true + resolve(m.content) + } + }) + + if (!found) resolve({ "name": "Unknown member" }) + }) + } + + /** + * @param {string} member + */ + findMember = async (member) => { + let found = false + let system = await this.getSystem() + return new Promise(async (resolve) => { + await asyncForEach(system, async (m) => { + if (m.content.name.includes(member)) { + found = true + resolve(m) + } + }) + + if (!found) resolve({"name": "Unknown member"}) + }) + } + + /** + * @param {string} member + * @param {function} callback + */ + findMemberCallback = async (member, callback) => { + await this.getSystem() + .then(async (system) => { + for (let i in system) { + if (system[i].content.name.includes(member)) { + await callback(system[i]) + return + } + } + }) + + } + + createMember = async (member) => { + let valid = await validate.validateSchema(schemas.memberSchema, member) + + if (valid) { + return axios.post(`${this.url}/v1/member/`, JSON.stringify(member), { + headers: this.header, + }) + .then((response) => response) + .catch(err => console.error(err.toJSON().message)); + } else { + let response = {} + response.data = { status: 'error', message: 'Invalid group schema' } + return response + } + } + + deleteMember = async (member) => { + return await axios.delete(`${this.url}/v1/member/${member}`, { + headers: this.header, + }) + .then((response) => response) + .catch(err => console.error(err.toJSON().message)); + } + + getFronters = async () => { + return await axios.get(`${this.url}/v1/fronters/`, { + headers: this.header, + }) + .then((response) => response.data) + .catch(err => console.error(err.toJSON().message)); + } +} + +asyncForEach = async (array, callback) => { + for (let index = 0; index < array.length; index++) { + await callback(array[index], index, array); + } +} + +module.exports = SimplyAPI \ No newline at end of file diff --git a/SimplyAPI/lib/Validate.js b/SimplyAPI/lib/Validate.js new file mode 100644 index 0000000..b0b2c62 --- /dev/null +++ b/SimplyAPI/lib/Validate.js @@ -0,0 +1,11 @@ +const Ajv = require('ajv') +const ajv = new Ajv() + +class Validate { + static validateSchema = async (schema, body) => { + const validate = ajv.compile(schema) + return validate(body) + + } +} +module.exports = Validate \ No newline at end of file diff --git a/SimplyAPI/package.json b/SimplyAPI/package.json new file mode 100644 index 0000000..51738f8 --- /dev/null +++ b/SimplyAPI/package.json @@ -0,0 +1,20 @@ +{ + "name": "simplyapi", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "padlocks", + "license": "ISC", + "optionalDependencies": { + "bufferutil": "^4.0.6", + "utf-8-validate": "^5.0.8" + }, + "dependencies": { + "ajv": "^8.10.0", + "axios": "^0.26.0", + "ws": "^8.5.0" + } +}