UNPKG

@azure/cosmos

Version:
161 lines (160 loc) 5.84 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Permission_exports = {}; __export(Permission_exports, { Permission: () => Permission }); module.exports = __toCommonJS(Permission_exports); var import_common = require("../../common/index.js"); var import_PermissionResponse = require("./PermissionResponse.js"); var import_diagnostics = require("../../utils/diagnostics.js"); class Permission { /** * @hidden * @param user - The parent {@link User}. * @param id - The id of the given {@link Permission}. */ constructor(user, id, clientContext) { this.user = user; this.id = id; this.clientContext = clientContext; } /** * Returns a reference URL to the resource. Used for linking in Permissions. */ get url() { return (0, import_common.createPermissionUri)(this.user.database.id, this.user.id, this.id); } /** * Read the {@link PermissionDefinition} of the given {@link Permission}. * @example * ```ts snippet:PermissionRead * import { CosmosClient } from "@azure/cosmos"; * * const endpoint = "https://your-account.documents.azure.com"; * const key = "<database account masterkey>"; * const client = new CosmosClient({ endpoint, key }); * const { database } = await client.databases.createIfNotExists({ id: "Test Database" }); * const user = database.user("<user-id>"); * * const { resource: permission } = await user.permission("<permission-id>").read(); * ``` */ async read(options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { const path = (0, import_common.getPathFromLink)(this.url); const id = (0, import_common.getIdFromLink)(this.url); const response = await this.clientContext.read({ path, resourceType: import_common.ResourceType.permission, resourceId: id, options, diagnosticNode }); return new import_PermissionResponse.PermissionResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Replace the given {@link Permission} with the specified {@link PermissionDefinition}. * @param body - The specified {@link PermissionDefinition}. * @example * ```ts snippet:PermissionReplace * import { CosmosClient } from "@azure/cosmos"; * * const endpoint = "https://your-account.documents.azure.com"; * const key = "<database account masterkey>"; * const client = new CosmosClient({ endpoint, key }); * const { database } = await client.databases.createIfNotExists({ id: "Test Database" }); * const user = database.user("<user-id>"); * const { resource: permission } = await user.permission("<permission-id>").read(); * permission.resource = "<new-resource-url>"; * * await user.permission("<permission-id>").replace(permission); * ``` */ async replace(body, options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { const err = {}; if (!(0, import_common.isResourceValid)(body, err)) { throw err; } const path = (0, import_common.getPathFromLink)(this.url); const id = (0, import_common.getIdFromLink)(this.url); const response = await this.clientContext.replace({ body, path, resourceType: import_common.ResourceType.permission, resourceId: id, options, diagnosticNode }); return new import_PermissionResponse.PermissionResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Delete the given {@link Permission}. * @example * ```ts snippet:PermissionDelete * import { CosmosClient } from "@azure/cosmos"; * * const endpoint = "https://your-account.documents.azure.com"; * const key = "<database account masterkey>"; * const client = new CosmosClient({ endpoint, key }); * const { database } = await client.databases.createIfNotExists({ id: "Test Database" }); * const user = database.user("<user-id>"); * * await user.permission("<permission-id>").delete(); * ``` */ async delete(options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { const path = (0, import_common.getPathFromLink)(this.url); const id = (0, import_common.getIdFromLink)(this.url); const response = await this.clientContext.delete({ path, resourceType: import_common.ResourceType.permission, resourceId: id, options, diagnosticNode }); return new import_PermissionResponse.PermissionResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Permission });