UNPKG

cloudkit-js

Version:

A JS library for managing a CloudKit container

188 lines (187 loc) 8.21 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CloudKitJs = void 0; const fs_1 = __importDefault(require("fs")); const sign_1 = require("./sign"); const request_1 = require("./request"); const url_1 = require("./url"); class CloudKitJs { constructor(initParams) { this.initParams = initParams; if (!initParams.privateKeyContents && !initParams.privateKeyPath) { throw new Error("You must specify either privateKeyPath or privateKeyContents"); } const signService = new sign_1.SignService(initParams.privateKeyContents || fs_1.default.readFileSync(initParams.privateKeyPath), initParams.keyId); this.requestService = new request_1.RequestService(signService); this.urlBuilder = new url_1.UrlBuilder({ containerName: initParams.containerName, environment: initParams.shouldUseProduction ? "production" : "development", database: (initParams === null || initParams === void 0 ? void 0 : initParams.database) || "public" }); } queryRecords(queryRecordOptions) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify(queryRecordOptions); return this.requestService.makeRequest(this.urlBuilder.getQueryRecordsPath(), payload); }); } getRecordByName(recordName, desiredKeys) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ records: [{ recordName, desiredKeys }] }); return this.requestService.makeRequest(this.urlBuilder.getLookupRecordsPath(), payload); }); } createRecord(createRecordOptions) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ operations: [{ operationType: "create", record: { recordType: createRecordOptions.recordType, recordName: createRecordOptions.recordName, fields: createRecordOptions.fields, } }] }); return this.requestService.makeRequest(this.urlBuilder.getModifyRecordsPath(), payload); }); } createRecords(records) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ operations: records.map(record => ({ operationType: "create", record: { recordType: record.recordType, fields: record.fields, recordName: record.recordName, } })) }); return this.requestService.makeRequest(this.urlBuilder.getModifyRecordsPath(), payload); }); } updateRecord(recordName, recordType, recordChangeTag, fields) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ operations: [{ operationType: "update", record: { recordName, recordType, fields, recordChangeTag, } }] }); return this.requestService.makeRequest(this.urlBuilder.getModifyRecordsPath(), payload); }); } forceUpdateRecord(recordName, recordType, fields) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ operations: [{ operationType: "forceUpdate", record: { recordName, recordType, fields } }] }); return this.requestService.makeRequest(this.urlBuilder.getModifyRecordsPath(), payload); }); } deleteRecord(deleteRecordOptions) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ operations: [{ operationType: "delete", record: { recordName: deleteRecordOptions.recordName, recordType: deleteRecordOptions.recordType, recordChangeTag: deleteRecordOptions.recordChangeTag } }] }); return this.requestService.makeRequest(this.urlBuilder.getModifyRecordsPath(), payload); }); } forceDeleteRecord(deleteRecordOptions) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ operations: [{ operationType: "forceDelete", record: { recordName: deleteRecordOptions.recordName, recordType: deleteRecordOptions.recordType, } }] }); return this.requestService.makeRequest(this.urlBuilder.getModifyRecordsPath(), payload); }); } forceDeleteRecords(recordType, recordNames) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ operations: recordNames.map(recordName => ({ operationType: "forceDelete", record: { recordName, recordType, } })) }); return this.requestService.makeRequest(this.urlBuilder.getModifyRecordsPath(), payload); }); } uploadAssetFromUrl(recordType, fieldName, fileUrl, recordName) { return __awaiter(this, void 0, void 0, function* () { // Download the asset from fileUrl const buffer = yield (yield fetch(fileUrl)).blob(); return this.uploadAsset(recordType, fieldName, buffer, recordName); }); } uploadAsset(recordType, fieldName, fileContents, recordName) { return __awaiter(this, void 0, void 0, function* () { const payload = JSON.stringify({ tokens: [{ recordName, recordType, fieldName }] }); const resp = yield this.requestService.makeRequest(this.urlBuilder.getUploadAssetsPath(), payload); const uploadUrl = resp.tokens[0].url; const assetResp = yield fetch(uploadUrl, { method: "POST", body: fileContents }); const imageResp = yield assetResp.json(); // Now we need to update the record with the asset return this.forceUpdateRecord(recordName, recordType, { [fieldName]: { value: imageResp.singleFile } }); }); } } exports.CloudKitJs = CloudKitJs;