@distilled.cloud/tailscale 1.0.0-rc.20 → 1.0.0-rc.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +3 -3
  2. package/lib/.lazy.json +1 -1
  3. package/lib/_bundle/sdk.js +1 -1
  4. package/lib/_bundle/sdk.js.map +1 -1
  5. package/lib/credentials.d.ts +17 -8
  6. package/lib/credentials.d.ts.map +1 -1
  7. package/lib/errors.d.ts +25 -0
  8. package/lib/errors.d.ts.map +1 -1
  9. package/lib/errors.js +1 -1
  10. package/lib/index.d.ts.map +1 -1
  11. package/lib/index.lazy.js.map +1 -1
  12. package/lib/pagination.d.ts.map +1 -1
  13. package/lib/protocol.d.ts +15 -8
  14. package/lib/protocol.d.ts.map +1 -1
  15. package/lib/retry.d.ts.map +1 -1
  16. package/lib/services/index.d.ts.map +1 -1
  17. package/lib/services/tailscale.d.ts +40 -86
  18. package/lib/services/tailscale.d.ts.map +1 -1
  19. package/lib/services/tailscale.js +130 -133
  20. package/lib/services/tailscale.js.map +1 -1
  21. package/lib/services.lazy/tailscale.js +3 -3
  22. package/lib/traits.d.ts.map +1 -1
  23. package/lib/unstable-services/index.d.ts +2 -0
  24. package/lib/unstable-services/index.d.ts.map +1 -0
  25. package/lib/unstable-services/index.js +1 -0
  26. package/lib/unstable-services/tailscale.d.ts +129 -0
  27. package/lib/unstable-services/tailscale.d.ts.map +1 -0
  28. package/lib/unstable-services/tailscale.js +2 -0
  29. package/lib/unstable-services/tailscale.js.map +1 -0
  30. package/lib/unstable-services.lazy/index.js +3 -0
  31. package/lib/unstable-services.lazy/tailscale.js +1 -0
  32. package/package.json +12 -5
  33. package/src/credentials.ts +78 -98
  34. package/src/errors.ts +23 -1
  35. package/src/protocol.ts +41 -44
  36. package/src/services/tailscale.ts +758 -811
  37. package/src/unstable-services/index.ts +2 -0
  38. package/src/unstable-services/tailscale.ts +319 -0
package/src/errors.ts CHANGED
@@ -19,7 +19,7 @@ export {
19
19
  export type { DefaultErrors } from "@distilled.cloud/core/errors";
20
20
 
21
21
  import * as Category from "@distilled.cloud/core/category";
22
- import { UnknownErrorFields } from "@distilled.cloud/core/errors";
22
+ import { API_ERRORS, UnknownErrorFields } from "@distilled.cloud/core/errors";
23
23
  import * as Schema from "@distilled.cloud/core/schema-lite-typed";
24
24
 
25
25
  export class UnknownTailscaleError
@@ -35,3 +35,25 @@ export class TailscaleParseError extends Schema.TaggedError<TailscaleParseError>
35
35
  cause: Schema.Unknown,
36
36
  },
37
37
  ).pipe(Category.withParseError) {}
38
+
39
+ /**
40
+ * The OAuth token exchange the credentials make failed: Tailscale could
41
+ * not be reached, refused the client credentials, answered with something
42
+ * that is not a token, or did not answer in time.
43
+ * `status` is the token endpoint's HTTP status, when it answered. It is a
44
+ * failure of a request at run time, so it is not a `ConfigError`.
45
+ */
46
+ export class TokenRequestFailed
47
+ extends /*@__PURE__*/ Schema.TaggedError<TokenRequestFailed>()("TokenRequestFailed", {
48
+ message: Schema.String,
49
+ status: /*@__PURE__*/ Schema.optional(Schema.Number),
50
+ }).pipe(Category.withAuthError) {}
51
+
52
+ /**
53
+ * The errors declared on the Tailscale service: any operation can return
54
+ * them, and every generated operation lists them as its `serviceErrors`.
55
+ * Each of core's status classes: the protocol builds the one for a failed
56
+ * response's status when the operation's own classes do not match.
57
+ */
58
+ export const TailscaleServiceErrors = API_ERRORS;
59
+ export type TailscaleServiceError = InstanceType<(typeof TailscaleServiceErrors)[number]>;
package/src/protocol.ts CHANGED
@@ -1,5 +1,4 @@
1
- import * as API from "@distilled.cloud/core/api";
2
- import type { ConfigError } from "@distilled.cloud/core/errors";
1
+ import type { DecodeArgs, ErrorOf, Protocol, ProtocolOf } from "@distilled.cloud/core/protocol";
3
2
  import {
4
3
  getAnn,
5
4
  getProps,
@@ -9,7 +8,7 @@ import {
9
8
  withResponseHeaders,
10
9
  } from "@distilled.cloud/core/protocol-http";
11
10
  import {
12
- makeRestProtocol,
11
+ restProtocol,
13
12
  rawResponseRootSymbol,
14
13
  rawResponseSymbol,
15
14
  validateSuccess,
@@ -19,19 +18,21 @@ import * as Effect from "effect/Effect";
19
18
  import type * as HttpClient from "effect/http/HttpClient";
20
19
  import type * as HttpClientError from "effect/http/HttpClientError";
21
20
  import type * as HttpClientResponse from "effect/http/HttpClientResponse";
22
- import * as Layer from "effect/Layer";
23
21
  import * as Redacted from "effect/Redacted";
24
22
  import type * as AST from "effect/SchemaAST";
25
- import { Credentials, type Config } from "./credentials.ts";
26
- import { API_ERRORS } from "./errors.ts";
27
- import { TailscaleParseError, UnknownTailscaleError } from "./errors.ts";
23
+ import { Credentials } from "./credentials.ts";
24
+ import {
25
+ TailscaleParseError,
26
+ UnknownTailscaleError,
27
+ type TailscaleServiceError,
28
+ } from "./errors.ts";
28
29
 
29
- export type TailscaleOpError =
30
- | InstanceType<(typeof API_ERRORS)[number]>
31
- | UnknownTailscaleError
32
- | ConfigError
33
- | HttpClientError.HttpClientError
34
- | TailscaleParseError;
30
+ /**
31
+ * What every generated Tailscale operation fails with besides its own
32
+ * errors: the service's errors, the protocol's, and what resolving the
33
+ * credentials fails with.
34
+ */
35
+ export type TailscaleOpError = TailscaleServiceError | ErrorOf<typeof TailscaleProtocol>;
35
36
 
36
37
  export type TailscaleOpContext = Credentials | HttpClient.HttpClient;
37
38
 
@@ -44,10 +45,6 @@ const isRawRoot = (outputAst: AST.AST): boolean =>
44
45
  const rawStringMember = (outputAst: AST.AST): AST.PropertySignature | undefined =>
45
46
  getProps(outputAst).find((prop) => hasPropAnn(prop, rawResponseSymbol) && isStringAst(prop.type));
46
47
 
47
- // The protocol's decode has no typed error channel; the generated operation
48
- // annotations reintroduce it for callers (same as core protocol-rest).
49
- const fail = (e: unknown): Effect.Effect<never> => Effect.fail(e) as Effect.Effect<never>;
50
-
51
48
  /**
52
49
  * A policy file is HuJSON text. It stays text even when it happens to be
53
50
  * valid JSON, so the core REST decode, which parses JSON bodies, can't read it.
@@ -56,7 +53,7 @@ const decodeRawString = (
56
53
  response: HttpClientResponse.HttpClientResponse,
57
54
  outputAst: AST.AST,
58
55
  member: AST.PropertySignature | undefined,
59
- ): Effect.Effect<unknown> =>
56
+ ): Effect.Effect<unknown, TailscaleParseError | HttpClientError.HttpClientError> =>
60
57
  Effect.flatMap(response.text, (text) =>
61
58
  validateSuccess(
62
59
  outputAst,
@@ -74,10 +71,10 @@ const decodeRawString = (
74
71
  },
75
72
  (info) => new TailscaleParseError(info),
76
73
  ),
77
- ).pipe(Effect.catch(fail));
74
+ );
78
75
 
79
- const TailscaleProtocolRest: Layer.Layer<API.Protocol> = /*@__PURE__*/ makeRestProtocol<Config>({
80
- credentials: /*@__PURE__*/ Effect.flatMap(Credentials, (resolve) => resolve),
76
+ const TailscaleProtocolRest = /*@__PURE__*/ restProtocol({
77
+ credentials: /*@__PURE__*/ Effect.flatten(Credentials),
81
78
  baseUrl: (creds) => creds.apiBaseUrl,
82
79
  headers: (creds) => ({
83
80
  Authorization: `Bearer ${Redacted.value(creds.token)}`,
@@ -86,31 +83,31 @@ const TailscaleProtocolRest: Layer.Layer<API.Protocol> = /*@__PURE__*/ makeRestP
86
83
  parseError: ({ body, cause }) => new TailscaleParseError({ body, cause }),
87
84
  });
88
85
 
86
+ const tailscaleProtocolMethods = {
87
+ encode: TailscaleProtocolRest.encode,
88
+ decode: (args: DecodeArgs) => {
89
+ if (args.response.status < 200 || args.response.status >= 300) {
90
+ return TailscaleProtocolRest.decode(args);
91
+ }
92
+ const { outputAst } = args;
93
+ if (isRawRoot(outputAst) && isStringAst(outputAst)) {
94
+ return decodeRawString(args.response, outputAst, undefined);
95
+ }
96
+ const member = rawStringMember(outputAst);
97
+ if (member !== undefined) return decodeRawString(args.response, outputAst, member);
98
+ if (isRawRoot(outputAst) && resolveNode(outputAst)._tag === "Arrays") {
99
+ return Effect.flatMap(args.response.text, (text) =>
100
+ text.trim() === "null" ? Effect.succeed([]) : TailscaleProtocolRest.decode(args),
101
+ );
102
+ }
103
+ return TailscaleProtocolRest.decode(args);
104
+ },
105
+ } satisfies Protocol<unknown, unknown>;
106
+
89
107
  /**
90
108
  * Core's REST protocol, plus two things the Tailscale API does differently on
91
109
  * a 2xx: policy files are HuJSON text, and an empty list can arrive as the
92
110
  * JSON `null` (a Go nil slice), which decodes as `[]`.
93
111
  */
94
- export const TailscaleProtocol: Layer.Layer<API.Protocol> = /*@__PURE__*/ Layer.effect(
95
- API.Protocol,
96
- Effect.map(API.Protocol, (rest) =>
97
- API.Protocol.of({
98
- encode: rest.encode,
99
- decode: (args) => {
100
- if (args.response.status < 200 || args.response.status >= 300) return rest.decode(args);
101
- const { outputAst } = args;
102
- if (isRawRoot(outputAst) && isStringAst(outputAst)) {
103
- return decodeRawString(args.response, outputAst, undefined);
104
- }
105
- const member = rawStringMember(outputAst);
106
- if (member !== undefined) return decodeRawString(args.response, outputAst, member);
107
- if (isRawRoot(outputAst) && resolveNode(outputAst)._tag === "Arrays") {
108
- return Effect.flatMap(args.response.text, (text) =>
109
- text.trim() === "null" ? Effect.succeed([]) : rest.decode(args),
110
- ).pipe(Effect.catch(fail));
111
- }
112
- return rest.decode(args);
113
- },
114
- }),
115
- ),
116
- ).pipe(Layer.provide(TailscaleProtocolRest));
112
+ export const TailscaleProtocol: ProtocolOf<typeof tailscaleProtocolMethods> =
113
+ tailscaleProtocolMethods;