feat: Add sync APIs for "Deno.permissions" (#17019)

This commit adds sync versions of async APIs to "Deno.permissions"
namespace.

Following APIs were added:
- "Deno.permissions.querySync"
- "Deno.permissions.requestSync"
- "Deno.permissions.revokeSync"
This commit is contained in:
Asher Gomez 2023-01-25 08:42:44 +09:00 committed by GitHub
parent f14ea3d4d4
commit 900929f65c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 284 additions and 15 deletions

View file

@ -4279,6 +4279,20 @@ declare namespace Deno {
*/
query(desc: PermissionDescriptor): Promise<PermissionStatus>;
/** Returns the current status of a permission.
*
* Note, if the permission is already granted, `request()` will not prompt
* the user again, therefore `querySync()` is only necessary if you are going
* to react differently existing permissions without wanting to modify them
* or prompt the user to modify them.
*
* ```ts
* const status = Deno.permissions.querySync({ name: "read", path: "/etc" });
* console.log(status.state);
* ```
*/
querySync(desc: PermissionDescriptor): PermissionStatus;
/** Revokes a permission, and resolves to the state of the permission.
*
* ```ts
@ -4290,6 +4304,17 @@ declare namespace Deno {
*/
revoke(desc: PermissionDescriptor): Promise<PermissionStatus>;
/** Revokes a permission, and returns the state of the permission.
*
* ```ts
* import { assert } from "https://deno.land/std/testing/asserts.ts";
*
* const status = Deno.permissions.revokeSync({ name: "run" });
* assert(status.state !== "granted")
* ```
*/
revokeSync(desc: PermissionDescriptor): PermissionStatus;
/** Requests the permission, and resolves to the state of the permission.
*
* If the permission is already granted, the user will not be prompted to
@ -4305,6 +4330,23 @@ declare namespace Deno {
* ```
*/
request(desc: PermissionDescriptor): Promise<PermissionStatus>;
/** Requests the permission, and returns the state of the permission.
*
* If the permission is already granted, the user will not be prompted to
* grant the permission again.
*
* ```ts
* const status = Deno.permissions.requestSync({ name: "env" });
* if (status.state === "granted") {
* console.log("'env' permission is granted.");
* } else {
* console.log("'env' permission is denied.");
* }
* ```
*/
requestSync(desc: PermissionDescriptor): PermissionStatus;
}
/** Deno's permission management API.
@ -4335,6 +4377,11 @@ declare namespace Deno {
* const status = await Deno.permissions.query({ name: "read", path: "/etc" });
* console.log(status.state);
* ```
*
* ```ts
* const status = Deno.permissions.querySync({ name: "read", path: "/etc" });
* console.log(status.state);
* ```
*
* ### Revoking
*
@ -4344,6 +4391,13 @@ declare namespace Deno {
* const status = await Deno.permissions.revoke({ name: "run" });
* assert(status.state !== "granted")
* ```
*
* ```ts
* import { assert } from "https://deno.land/std/testing/asserts.ts";
*
* const status = Deno.permissions.revokeSync({ name: "run" });
* assert(status.state !== "granted")
* ```
*
* ### Requesting
*
@ -4355,6 +4409,15 @@ declare namespace Deno {
* console.log("'env' permission is denied.");
* }
* ```
*
* ```ts
* const status = Deno.permissions.requestSync({ name: "env" });
* if (status.state === "granted") {
* console.log("'env' permission is granted.");
* } else {
* console.log("'env' permission is denied.");
* }
* ```
*
* @category Permissions
*/