|
@@ -0,0 +1,418 @@
|
|
|
+
|
|
|
+ __type: string;
|
|
|
+ className: string;
|
|
|
+ objectId?: string;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+export class CloudObject {
|
|
|
+ id: string | null = null;
|
|
|
+ className: string;
|
|
|
+ createdAt: string | null = null;
|
|
|
+ updatedAt: string | null = null;
|
|
|
+ data: Record<string, any> = {};
|
|
|
+
|
|
|
+
|
|
|
+ QuestionnaireId?: string;
|
|
|
+ QuestionId?: string;
|
|
|
+ OptionId?: string;
|
|
|
+ _UserId?: string;
|
|
|
+
|
|
|
+ constructor(className: string) {
|
|
|
+ this.className = className;
|
|
|
+ }
|
|
|
+
|
|
|
+ toPointer(): Pointer {
|
|
|
+
|
|
|
+ let pointer: Pointer = { "__type": "Pointer", "className": this.className };
|
|
|
+
|
|
|
+
|
|
|
+ switch (this.className) {
|
|
|
+ case "Questionnaire":
|
|
|
+ pointer.objectId = this.QuestionnaireId || "";
|
|
|
+ break;
|
|
|
+ case "Question":
|
|
|
+ pointer.objectId = this.QuestionId || "";
|
|
|
+ break;
|
|
|
+ case "Option":
|
|
|
+ pointer.objectId = this.OptionId || "";
|
|
|
+ break;
|
|
|
+ case "_User":
|
|
|
+ pointer.objectId = this._UserId || "";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ pointer.objectId = this.id || "";
|
|
|
+ }
|
|
|
+
|
|
|
+ return pointer;
|
|
|
+ }
|
|
|
+
|
|
|
+ set(json: Record<string, any>) {
|
|
|
+ Object.keys(json).forEach(key => {
|
|
|
+ if (["objectId", "id", "createdAt", "updatedAt", "ACL"].includes(key)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.data[key] = json[key];
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ get(key: string) {
|
|
|
+ return this.data[key] || null;
|
|
|
+ }
|
|
|
+
|
|
|
+ async save(): Promise<this> {
|
|
|
+ let method = "POST";
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
|
|
|
+
|
|
|
+
|
|
|
+ if (this.id) {
|
|
|
+ url += `/${this.id}`;
|
|
|
+ method = "PUT";
|
|
|
+ }
|
|
|
+
|
|
|
+ const body = JSON.stringify(this.data);
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "content-type": "application/json;charset=UTF-8",
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ body: body,
|
|
|
+ method: method,
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response.json();
|
|
|
+ if (result?.error) {
|
|
|
+ console.error(result.error);
|
|
|
+ }
|
|
|
+ if (result?.objectId) {
|
|
|
+ this.id = result.objectId;
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ async destroy(): Promise<boolean> {
|
|
|
+ if (!this.id) return false;
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/${this.className}/${this.id}`, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "DELETE",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+ const result = await response.json();
|
|
|
+ if (result) {
|
|
|
+ this.id = null;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export class CloudQuery {
|
|
|
+ className: string;
|
|
|
+ whereOptions: Record<string, any> = {};
|
|
|
+
|
|
|
+ constructor(className: string) {
|
|
|
+ this.className = className;
|
|
|
+ }
|
|
|
+
|
|
|
+ greaterThan(key: string, value: any) {
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$gt"] = value;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ greaterThanAndEqualTo(key: string, value: any) {
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$gte"] = value;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ lessThan(key: string, value: any) {
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$lt"] = value;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ lessThanAndEqualTo(key: string, value: any) {
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$lte"] = value;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ equalTo(key: string, value: any) {
|
|
|
+ this.whereOptions[key] = value;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ async get(id: string): Promise<Record<string, any>> {
|
|
|
+ const url = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}`;
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "GET",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+ const json = await response.json();
|
|
|
+ return json || {};
|
|
|
+ }
|
|
|
+
|
|
|
+ async find(): Promise<CloudObject[]> {
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
+
|
|
|
+ if (Object.keys(this.whereOptions).length) {
|
|
|
+ const whereStr = JSON.stringify(this.whereOptions);
|
|
|
+ url += `where=${encodeURIComponent(whereStr)}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "GET",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+ const json = await response.json();
|
|
|
+ return json?.results.map((item: any) => {
|
|
|
+ const cloudObject = new CloudObject(this.className);
|
|
|
+ cloudObject.set(item);
|
|
|
+ cloudObject.id = item.objectId;
|
|
|
+ cloudObject.createdAt = item.createdAt;
|
|
|
+ cloudObject.updatedAt = item.updatedAt;
|
|
|
+ return cloudObject;
|
|
|
+ }) || [];
|
|
|
+ }
|
|
|
+
|
|
|
+ async first(): Promise<CloudObject | null> {
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
+
|
|
|
+ if (Object.keys(this.whereOptions).length) {
|
|
|
+ const whereStr = JSON.stringify(this.whereOptions);
|
|
|
+ url += `where=${encodeURIComponent(whereStr)}&limit=1`;
|
|
|
+ } else {
|
|
|
+ url += `limit=1`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "GET",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+ const json = await response.json();
|
|
|
+ const exists = json?.results?.[0] || null;
|
|
|
+ if (exists) {
|
|
|
+ const cloudObject = new CloudObject(this.className);
|
|
|
+ cloudObject.set(exists);
|
|
|
+ cloudObject.id = exists.objectId;
|
|
|
+ cloudObject.createdAt = exists.createdAt;
|
|
|
+ cloudObject.updatedAt = exists.updatedAt;
|
|
|
+ return cloudObject;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}*/
|
|
|
+
|
|
|
+export interface Pointer {
|
|
|
+ __type: string;
|
|
|
+ className: string;
|
|
|
+ objectId?: string;
|
|
|
+}
|
|
|
+export class CloudObject {
|
|
|
+ id: string | null = null;
|
|
|
+ className: string;
|
|
|
+ data: Record<string, any> = {};
|
|
|
+ createdAt: string | null = null;
|
|
|
+ updatedAt: string | null = null;
|
|
|
+
|
|
|
+ constructor(className: string) {
|
|
|
+ this.className = className;
|
|
|
+ }
|
|
|
+
|
|
|
+ toPointer() {
|
|
|
+ return { "__type": "Pointer", "className": this.className, "objectId": this.id };
|
|
|
+ }
|
|
|
+
|
|
|
+ set(json: Record<string, any>) {
|
|
|
+ Object.keys(json).forEach(key => {
|
|
|
+ if (["objectId", "id", "createdAt", "updatedAt", "ACL"].includes(key)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.data[key] = json[key];
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ get(key: string) {
|
|
|
+ return this.data[key] || null;
|
|
|
+ }
|
|
|
+
|
|
|
+ async save(): Promise<CloudObject> {
|
|
|
+ let method = "POST";
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
|
|
|
+
|
|
|
+
|
|
|
+ if (this.id) {
|
|
|
+ url += `/${this.id}`;
|
|
|
+ method = "PUT";
|
|
|
+ }
|
|
|
+
|
|
|
+ const body = JSON.stringify(this.data);
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json;charset=UTF-8",
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ body,
|
|
|
+ method,
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response.json();
|
|
|
+ if (result?.error) {
|
|
|
+ console.error(result.error);
|
|
|
+ }
|
|
|
+ if (result?.objectId) {
|
|
|
+ this.id = result.objectId;
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ async destroy(): Promise<boolean> {
|
|
|
+ if (!this.id) return false;
|
|
|
+
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/${this.className}/${this.id}`, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "DELETE",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response?.json();
|
|
|
+ if (result) {
|
|
|
+ this.id = null;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+export class CloudQuery {
|
|
|
+ className: string;
|
|
|
+ whereOptions: Record<string, any> = {};
|
|
|
+
|
|
|
+ constructor(className: string) {
|
|
|
+ this.className = className;
|
|
|
+ }
|
|
|
+
|
|
|
+ greaterThan(key: string, value: any) {
|
|
|
+ this.whereOptions[key] = { "$gt": value };
|
|
|
+ }
|
|
|
+
|
|
|
+ greaterThanAndEqualTo(key: string, value: any) {
|
|
|
+ this.whereOptions[key] = { "$gte": value };
|
|
|
+ }
|
|
|
+
|
|
|
+ lessThan(key: string, value: any) {
|
|
|
+ this.whereOptions[key] = { "$lt": value };
|
|
|
+ }
|
|
|
+
|
|
|
+ lessThanAndEqualTo(key: string, value: any) {
|
|
|
+ this.whereOptions[key] = { "$lte": value };
|
|
|
+ }
|
|
|
+
|
|
|
+ equalTo(key: string, value: any) {
|
|
|
+ this.whereOptions[key] = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ containedIn(key: string, valueArray: any[]) {
|
|
|
+ this.whereOptions[key] = { "$in": valueArray };
|
|
|
+ }
|
|
|
+
|
|
|
+ async get(id: string): Promise<Record<string, any>> {
|
|
|
+ const url = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}?`;
|
|
|
+
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "GET",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+
|
|
|
+ const json = await response.json();
|
|
|
+ return json || {};
|
|
|
+ }
|
|
|
+
|
|
|
+ async find(): Promise<CloudObject[]> {
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
+
|
|
|
+ if (Object.keys(this.whereOptions).length) {
|
|
|
+ const whereStr = JSON.stringify(this.whereOptions);
|
|
|
+ url += `where=${encodeURIComponent(whereStr)}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2BDKY\"",
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "GET",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+
|
|
|
+ const json = await response.json();
|
|
|
+ return json?.results.map((item: any) => {
|
|
|
+ const cloudObject = new CloudObject(this.className);
|
|
|
+ cloudObject.set(item);
|
|
|
+ cloudObject.id = item.objectId;
|
|
|
+ return cloudObject;
|
|
|
+ }) || [];
|
|
|
+ }
|
|
|
+
|
|
|
+ async first(): Promise<CloudObject | null> {
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
+
|
|
|
+ if (Object.keys(this.whereOptions).length) {
|
|
|
+ const whereStr = JSON.stringify(this.whereOptions);
|
|
|
+ url += `where=${encodeURIComponent(whereStr)}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: {
|
|
|
+ "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
+ "x-parse-application-id": "dev"
|
|
|
+ },
|
|
|
+ method: "GET",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+
|
|
|
+ const json = await response.json();
|
|
|
+ const exists = json?.results?.[0] || null;
|
|
|
+ if (exists) {
|
|
|
+ const existsObject = new CloudObject(this.className);
|
|
|
+ existsObject.set(exists);
|
|
|
+ existsObject.id = exists.objectId;
|
|
|
+ existsObject.createdAt = exists.createdAt;
|
|
|
+ existsObject.updatedAt = exists.updatedAt;
|
|
|
+ return existsObject;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|