@distilled.cloud/questrade 0.0.0 → 1.0.0-rc.19
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.
- package/LICENSE +201 -0
- package/README.md +83 -4
- package/lib/.lazy.json +1 -0
- package/lib/_bundle/lazy.js +4 -0
- package/lib/_bundle/sdk.js +2 -0
- package/lib/_bundle/sdk.js.map +1 -0
- package/lib/credentials.d.ts +49 -0
- package/lib/credentials.d.ts.map +1 -0
- package/lib/credentials.js +1 -0
- package/lib/errors.d.ts +50 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +1 -0
- package/lib/index.d.ts +37 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -0
- package/lib/index.lazy.js +6 -0
- package/lib/index.lazy.js.map +1 -0
- package/lib/protocol.d.ts +19 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +1 -0
- package/lib/retry.d.ts +50 -0
- package/lib/retry.d.ts.map +1 -0
- package/lib/retry.js +1 -0
- package/lib/services/index.d.ts +2 -0
- package/lib/services/index.d.ts.map +1 -0
- package/lib/services/index.js +1 -0
- package/lib/services/questrade.d.ts +1228 -0
- package/lib/services/questrade.d.ts.map +1 -0
- package/lib/services/questrade.js +42 -0
- package/lib/services/questrade.js.map +1 -0
- package/lib/services.lazy/index.js +3 -0
- package/lib/services.lazy/questrade.js +3 -0
- package/lib/traits.d.ts +10 -0
- package/lib/traits.d.ts.map +1 -0
- package/lib/traits.js +1 -0
- package/package.json +76 -2
- package/src/credentials.ts +216 -0
- package/src/errors.ts +77 -0
- package/src/index.ts +36 -0
- package/src/protocol.ts +55 -0
- package/src/retry.ts +69 -0
- package/src/services/index.ts +2 -0
- package/src/services/questrade.ts +2666 -0
- package/src/traits.ts +44 -0
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type * as API from "@distilled.cloud/core/api";
|
|
2
|
+
import type { ConfigError, InvalidInput } from "@distilled.cloud/core/errors";
|
|
3
|
+
import { makeRestProtocol } from "@distilled.cloud/core/protocol-rest";
|
|
4
|
+
import type { SdkError } from "@distilled.cloud/core/sdk-error";
|
|
5
|
+
/**
|
|
6
|
+
* Questrade protocol -- the shared REST protocol with Questrade's auth.
|
|
7
|
+
*
|
|
8
|
+
* Every call goes to the API server the access token was issued for
|
|
9
|
+
* (`api_server` from the token redemption, see credentials.ts) with
|
|
10
|
+
* `Authorization: Bearer <access token>`; the operation routes carry their
|
|
11
|
+
* `/v1` prefix.
|
|
12
|
+
*
|
|
13
|
+
* Responses are plain JSON with no success envelope. Failures are
|
|
14
|
+
* `{ code, message }` with a numeric code; core's default error envelope
|
|
15
|
+
* reads both, and the factory maps the status to core's shared classes, then
|
|
16
|
+
* {@link UnknownQuestradeError}.
|
|
17
|
+
*/
|
|
18
|
+
import * as Effect from "effect/Effect";
|
|
19
|
+
import type * as HttpClient from "effect/http/HttpClient";
|
|
20
|
+
import type * as HttpClientError from "effect/http/HttpClientError";
|
|
21
|
+
import type * as Layer from "effect/Layer";
|
|
22
|
+
import * as Redacted from "effect/Redacted";
|
|
23
|
+
import { Credentials, type Config } from "./credentials.ts";
|
|
24
|
+
import { QuestradeParseError, UnknownQuestradeError, type DefaultErrors } from "./errors.ts";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Error channel shared by every generated Questrade operation. Generated
|
|
28
|
+
* service files annotate operations with `API.OperationMethod<I, O,
|
|
29
|
+
* QuestradeOpError, QuestradeOpContext>` explicitly so the compiler never
|
|
30
|
+
* infers these back out of the schema generics.
|
|
31
|
+
*/
|
|
32
|
+
export type QuestradeOpError =
|
|
33
|
+
| DefaultErrors
|
|
34
|
+
| ConfigError
|
|
35
|
+
| HttpClientError.HttpClientError
|
|
36
|
+
| InvalidInput
|
|
37
|
+
| SdkError;
|
|
38
|
+
|
|
39
|
+
/** Context (requirements) shared by every generated Questrade operation. */
|
|
40
|
+
export type QuestradeOpContext = Credentials | HttpClient.HttpClient;
|
|
41
|
+
|
|
42
|
+
export const QuestradeProtocol: Layer.Layer<API.Protocol> = /*@__PURE__*/ makeRestProtocol<Config>({
|
|
43
|
+
// Resolved on the calling fiber per request, so a refreshed token is
|
|
44
|
+
// picked up without rebuilding the layer.
|
|
45
|
+
credentials: /*@__PURE__*/ Effect.flatMap(Credentials, (resolve) => resolve),
|
|
46
|
+
baseUrl: (creds) => creds.apiServer,
|
|
47
|
+
headers: (creds) => ({ authorization: `Bearer ${Redacted.value(creds.accessToken)}` }),
|
|
48
|
+
unknownError: ({ code, message, body }) =>
|
|
49
|
+
new UnknownQuestradeError({
|
|
50
|
+
code: code !== undefined ? String(code) : undefined,
|
|
51
|
+
message,
|
|
52
|
+
body,
|
|
53
|
+
}),
|
|
54
|
+
parseError: ({ body, cause }) => new QuestradeParseError({ body, cause }),
|
|
55
|
+
});
|
package/src/retry.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as Retries from "@distilled.cloud/core/retry";
|
|
2
|
+
/**
|
|
3
|
+
* Questrade retry surface -- a veneer over `@distilled.cloud/core/retry`.
|
|
4
|
+
*
|
|
5
|
+
* The `Retry` service tag is threaded into every generated operation via
|
|
6
|
+
* `API.make({ retry: Retry })`, so a caller-installed policy applies to all
|
|
7
|
+
* Questrade calls below it and core's `makeDefault` is the fallback when none is
|
|
8
|
+
* provided.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import * as Questrade from "@distilled.cloud/questrade";
|
|
13
|
+
*
|
|
14
|
+
* myEffect.pipe(Questrade.Retry.transient);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
import * as Context from "effect/Context";
|
|
18
|
+
import * as Effect from "effect/Effect";
|
|
19
|
+
import * as Layer from "effect/Layer";
|
|
20
|
+
|
|
21
|
+
export type Options = Retries.Options;
|
|
22
|
+
export type Factory = Retries.Factory;
|
|
23
|
+
export type Policy = Retries.Policy;
|
|
24
|
+
|
|
25
|
+
/** Context tag for configuring retry behavior of Questrade API calls. */
|
|
26
|
+
export class Retry extends /*@__PURE__*/ Context.Service<Retry, Policy>()("QuestradeRetry") {}
|
|
27
|
+
|
|
28
|
+
/** Provides a custom retry policy to every Questrade API call below it. */
|
|
29
|
+
export const policy: {
|
|
30
|
+
(
|
|
31
|
+
options: Options,
|
|
32
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
33
|
+
(
|
|
34
|
+
factory: Factory,
|
|
35
|
+
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Retry>>;
|
|
36
|
+
} = (optionsOrFactory: Options | Factory) => Effect.provide(Layer.succeed(Retry, optionsOrFactory));
|
|
37
|
+
|
|
38
|
+
/** Disables all automatic retries. */
|
|
39
|
+
export const none: <A, E, R>(
|
|
40
|
+
effect: Effect.Effect<A, E, R>,
|
|
41
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ Effect.provide(
|
|
42
|
+
/*@__PURE__*/ Layer.succeed(Retry, { while: () => false }),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The default retry policy (core's): transient/throttling/retryable errors,
|
|
47
|
+
* capped exponential backoff with jitter, server `retryAfter` hints honored
|
|
48
|
+
* with precedence.
|
|
49
|
+
*/
|
|
50
|
+
export const makeDefault: Factory = Retries.makeDefault;
|
|
51
|
+
|
|
52
|
+
export const jittered = Retries.jittered;
|
|
53
|
+
export const capped = Retries.capped;
|
|
54
|
+
|
|
55
|
+
/** Retry options that retry all throttling errors indefinitely. */
|
|
56
|
+
export const throttlingOptions: Options = Retries.throttlingOptions;
|
|
57
|
+
|
|
58
|
+
/** Retries all throttling errors indefinitely (honoring server hints). */
|
|
59
|
+
export const throttling: <A, E, R>(
|
|
60
|
+
effect: Effect.Effect<A, E, R>,
|
|
61
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ policy(Retries.throttlingFactory);
|
|
62
|
+
|
|
63
|
+
/** Retry options that retry all transient errors indefinitely. */
|
|
64
|
+
export const transientOptions: Options = Retries.transientOptions;
|
|
65
|
+
|
|
66
|
+
/** Retries all transient errors indefinitely. */
|
|
67
|
+
export const transient: <A, E, R>(
|
|
68
|
+
effect: Effect.Effect<A, E, R>,
|
|
69
|
+
) => Effect.Effect<A, E, Exclude<R, Retry>> = /*@__PURE__*/ policy(Retries.transientFactory);
|