The best way to control Claude Code, Codex and more from any computer or phone

main
ts 154 lines 3.99 KB
Raw
1 /**
2 * Effect service for the browser Permissions API.
3 *
4 * This module defines a `Permissions` service backed by
5 * `navigator.permissions`. The service exposes `query`, which returns the
6 * browser `PermissionStatus` for a permission name. Failed browser operations
7 * are represented as `PermissionsError` values with `InvalidStateError` or
8 * `TypeError` reasons, and `layer` provides the browser-backed service.
9 *
10 * @stability unstable
11 * @since 4.0.0
12 */
13 import * as Context from "effect/Context"
14 import * as Data from "effect/Data"
15 import * as Effect from "effect/Effect"
16 import * as Layer from "effect/Layer"
17
18 const TypeId = "~@effect/platform-browser/Permissions"
19 const ErrorTypeId = "~@effect/platform-browser/Permissions/PermissionsError"
20
21 /**
22 * Wrapper on the Permission API (`navigator.permissions`) with methods for
23 * querying status of permissions.
24 *
25 * @stability unstable
26 * @category services
27 * @since 4.0.0
28 */
29 export interface Permissions {
30 readonly [TypeId]: typeof TypeId
31
32 /**
33 * Returns the state of a user permission on the global scope.
34 */
35 readonly query: <Name extends PermissionName>(
36 name: Name
37 ) => Effect.Effect<
38 // `name` is identical to the name passed to Permissions.query
39 // https://developer.mozilla.org/en-US/docs/Web/API/PermissionStatus
40 Omit<PermissionStatus, "name"> & { name: Name },
41 PermissionsError
42 >
43 }
44
45 /**
46 * Error reason for an `InvalidStateError` raised by the browser Permissions API.
47 *
48 * @stability unstable
49 * @category errors
50 * @since 4.0.0
51 */
52 export class PermissionsInvalidStateError extends Data.TaggedError("InvalidStateError")<{
53 readonly cause: unknown
54 }> {
55 override get message(): string {
56 return this._tag
57 }
58 }
59
60 /**
61 * Error reason for a `TypeError` raised by the browser Permissions API.
62 *
63 * @stability unstable
64 * @category errors
65 * @since 4.0.0
66 */
67 export class PermissionsTypeError extends Data.TaggedError("TypeError")<{
68 readonly cause: unknown
69 }> {
70 override get message(): string {
71 return this._tag
72 }
73 }
74
75 /**
76 * Union of browser Permissions API error reasons represented by the service.
77 *
78 * @stability unstable
79 * @category errors
80 * @since 4.0.0
81 */
82 export type PermissionsErrorReason = PermissionsInvalidStateError | PermissionsTypeError
83
84 /**
85 * Tagged error wrapping a browser Permissions API failure reason.
86 *
87 * @stability unstable
88 * @category errors
89 * @since 4.0.0
90 */
91 export class PermissionsError extends Data.TaggedError("PermissionsError")<{
92 readonly reason: PermissionsErrorReason
93 }> {
94 constructor(props: { readonly reason: PermissionsErrorReason }) {
95 super({
96 ...props,
97 cause: props.reason.cause
98 } as any)
99 }
100
101 readonly [ErrorTypeId] = ErrorTypeId
102
103 override get message(): string {
104 return this.reason.message
105 }
106 }
107
108 /**
109 * Service tag for browser permission querying.
110 *
111 * **When to use**
112 *
113 * Use when you need to require or provide browser permission querying through
114 * Effect's context.
115 *
116 * @stability unstable
117 * @category services
118 * @since 4.0.0
119 */
120 export const Permissions: Context.Service<Permissions, Permissions> = Context.Service<Permissions>(TypeId)
121
122 /**
123 * Provides the `Permissions` service using the browser `navigator.permissions` API.
124 *
125 * **When to use**
126 *
127 * Use when you need a live browser `Permissions` service backed by the ambient
128 * `navigator.permissions` implementation.
129 *
130 * **Details**
131 *
132 * `query` delegates to `navigator.permissions.query({ name })` and wraps
133 * rejected browser operations in `PermissionsError`.
134 *
135 * @stability unstable
136 * @category layers
137 * @since 4.0.0
138 */
139 export const layer: Layer.Layer<Permissions> = Layer.succeed(
140 Permissions,
141 Permissions.of({
142 [TypeId]: TypeId,
143 query: (name) =>
144 Effect.tryPromise({
145 try: () => navigator.permissions.query({ name }) as Promise<any>,
146 catch: (cause) =>
147 new PermissionsError({
148 reason: cause instanceof DOMException
149 ? new PermissionsInvalidStateError({ cause })
150 : new PermissionsTypeError({ cause })
151 })
152 })
153 })
154 )