temporarily add SAPI
This commit is contained in:
parent
19f49e4a9d
commit
5622037719
9 changed files with 452 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,4 +1,3 @@
|
|||
/SimplyAPI
|
||||
/test.js
|
||||
/node_modules
|
||||
/.vscode
|
||||
|
|
|
|||
61
SimplyAPI/examples/groups.js
Normal file
61
SimplyAPI/examples/groups.js
Normal file
|
|
@ -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()
|
||||
58
SimplyAPI/examples/members.js
Normal file
58
SimplyAPI/examples/members.js
Normal file
|
|
@ -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()
|
||||
26
SimplyAPI/examples/other.js
Normal file
26
SimplyAPI/examples/other.js
Normal file
|
|
@ -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()
|
||||
6
SimplyAPI/index.js
Normal file
6
SimplyAPI/index.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
const SimplyAPI = require('./lib/SimplyAPI')
|
||||
|
||||
SimplyAPI.Validate = require('./lib/Validate')
|
||||
SimplyAPI.Schemas = require('./lib/Schemas')
|
||||
|
||||
module.exports = SimplyAPI;
|
||||
100
SimplyAPI/lib/Schemas.js
Normal file
100
SimplyAPI/lib/Schemas.js
Normal file
|
|
@ -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
|
||||
}
|
||||
170
SimplyAPI/lib/SimplyAPI.js
Normal file
170
SimplyAPI/lib/SimplyAPI.js
Normal file
|
|
@ -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
|
||||
11
SimplyAPI/lib/Validate.js
Normal file
11
SimplyAPI/lib/Validate.js
Normal file
|
|
@ -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
|
||||
20
SimplyAPI/package.json
Normal file
20
SimplyAPI/package.json
Normal file
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue