@encore.dev/sdk 2.0.0-beta.7 → 2.0.0-beta.8
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/README.md +55 -0
- package/app_meta.ts +8 -0
- package/config/config.test.ts +10 -3
- package/config/config.ts +75 -21
- package/config/mod.ts +4 -3
- package/config/secrets.ts +72 -55
- package/cron/mod.ts +32 -0
- package/dist/app_meta.d.ts +7 -0
- package/dist/app_meta.d.ts.map +1 -0
- package/dist/app_meta.js +1 -0
- package/dist/app_meta.js.map +1 -1
- package/dist/config/config.d.ts +44 -18
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +43 -18
- package/dist/config/config.js.map +1 -1
- package/dist/config/mod.d.ts +5 -4
- package/dist/config/mod.d.ts.map +1 -0
- package/dist/config/mod.js +2 -2
- package/dist/config/mod.js.map +1 -1
- package/dist/config/secrets.d.ts +62 -39
- package/dist/config/secrets.d.ts.map +1 -0
- package/dist/config/secrets.js +21 -37
- package/dist/config/secrets.js.map +1 -1
- package/dist/config/value.d.ts +1 -0
- package/dist/config/value.d.ts.map +1 -0
- package/dist/cron/mod.d.ts +31 -0
- package/dist/cron/mod.d.ts.map +1 -0
- package/dist/cron/mod.js +23 -0
- package/dist/cron/mod.js.map +1 -1
- package/dist/gateway/errors.d.ts +101 -0
- package/dist/gateway/errors.d.ts.map +1 -0
- package/dist/gateway/errors.js +146 -0
- package/dist/gateway/errors.js.map +1 -0
- package/dist/gateway/match.d.ts +127 -0
- package/dist/gateway/match.d.ts.map +1 -0
- package/dist/gateway/match.js +206 -0
- package/dist/gateway/match.js.map +1 -0
- package/dist/gateway/middleware.d.ts +108 -0
- package/dist/gateway/middleware.d.ts.map +1 -0
- package/dist/gateway/middleware.js +9 -0
- package/dist/gateway/middleware.js.map +1 -0
- package/dist/gateway/mod.d.ts +129 -0
- package/dist/gateway/mod.d.ts.map +1 -0
- package/dist/gateway/mod.js +187 -0
- package/dist/gateway/mod.js.map +1 -0
- package/dist/gateway/request.d.ts +304 -0
- package/dist/gateway/request.d.ts.map +1 -0
- package/dist/gateway/request.js +403 -0
- package/dist/gateway/request.js.map +1 -0
- package/dist/gateway/serve.d.ts +114 -0
- package/dist/gateway/serve.d.ts.map +1 -0
- package/dist/gateway/serve.js +510 -0
- package/dist/gateway/serve.js.map +1 -0
- package/dist/internal/clients/call.d.ts +147 -0
- package/dist/internal/clients/call.d.ts.map +1 -0
- package/dist/internal/clients/call.js +274 -0
- package/dist/internal/clients/call.js.map +1 -0
- package/dist/internal/clients/grpc.d.ts +90 -0
- package/dist/internal/clients/grpc.d.ts.map +1 -0
- package/dist/internal/clients/grpc.js +355 -0
- package/dist/internal/clients/grpc.js.map +1 -0
- package/dist/internal/clients/mod.d.ts +81 -0
- package/dist/internal/clients/mod.d.ts.map +1 -0
- package/dist/internal/clients/mod.js +237 -0
- package/dist/internal/clients/mod.js.map +1 -0
- package/dist/internal/fetch.d.ts +54 -0
- package/dist/internal/fetch.d.ts.map +1 -0
- package/dist/internal/fetch.js +240 -0
- package/dist/internal/fetch.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +2 -0
- package/dist/internal/otel/backend.d.ts.map +1 -0
- package/dist/internal/otel/context.d.ts +1 -0
- package/dist/internal/otel/context.d.ts.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +1 -0
- package/dist/internal/otel/context_manager.d.ts.map +1 -0
- package/dist/internal/otel/encode.d.ts +2 -0
- package/dist/internal/otel/encode.d.ts.map +1 -0
- package/dist/internal/otel/index.d.ts +2 -2
- package/dist/internal/otel/index.d.ts.map +1 -0
- package/dist/internal/otel/index.js +1 -2
- package/dist/internal/otel/index.js.map +1 -1
- package/dist/internal/otel/meter.d.ts +1 -0
- package/dist/internal/otel/meter.d.ts.map +1 -0
- package/dist/internal/otel/propagator.d.ts +1 -0
- package/dist/internal/otel/propagator.d.ts.map +1 -0
- package/dist/internal/otel/provider.d.ts +1 -0
- package/dist/internal/otel/provider.d.ts.map +1 -0
- package/dist/internal/otel/span.d.ts +4 -0
- package/dist/internal/otel/span.d.ts.map +1 -0
- package/dist/internal/otel/span.js +5 -0
- package/dist/internal/otel/span.js.map +1 -1
- package/dist/internal/otel/tracer.d.ts +1 -0
- package/dist/internal/otel/tracer.d.ts.map +1 -0
- package/dist/internal/otel/tracer.js +11 -5
- package/dist/internal/otel/tracer.js.map +1 -1
- package/dist/internal/reqfields.d.ts +150 -0
- package/dist/internal/reqfields.d.ts.map +1 -0
- package/dist/internal/reqfields.js +312 -0
- package/dist/internal/reqfields.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +1 -0
- package/dist/internal/runtime/meter.d.ts.map +1 -0
- package/dist/internal/runtime/mod.d.ts +6 -0
- package/dist/internal/runtime/mod.d.ts.map +1 -0
- package/dist/internal/runtime/mod.js +32 -2
- package/dist/internal/runtime/mod.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +160 -16
- package/dist/internal/runtime/napi/napi.d.cts +474 -29
- package/dist/internal/runtime/shutdown.d.ts +5 -0
- package/dist/internal/runtime/shutdown.d.ts.map +1 -0
- package/dist/internal/runtime/shutdown.js +9 -1
- package/dist/internal/runtime/shutdown.js.map +1 -1
- package/dist/internal/testing/runner.d.ts +2 -0
- package/dist/internal/testing/runner.d.ts.map +1 -0
- package/dist/internal/testing/runner.js +10 -0
- package/dist/internal/testing/runner.js.map +1 -0
- package/dist/internal/testing/scope.d.ts +81 -0
- package/dist/internal/testing/scope.d.ts.map +1 -0
- package/dist/internal/testing/scope.js +237 -0
- package/dist/internal/testing/scope.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +1 -0
- package/dist/internal/utils/constraints.d.ts.map +1 -0
- package/dist/log/index.d.ts +1 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/logger.d.ts +2 -0
- package/dist/log/logger.d.ts.map +1 -0
- package/dist/log/runtime.d.ts +2 -0
- package/dist/log/runtime.d.ts.map +1 -0
- package/dist/log/stderr.d.ts +1 -0
- package/dist/log/stderr.d.ts.map +1 -0
- package/dist/metrics/mod.d.ts +45 -118
- package/dist/metrics/mod.d.ts.map +1 -0
- package/dist/metrics/mod.js +44 -179
- package/dist/metrics/mod.js.map +1 -1
- package/dist/mod.d.ts +1 -2
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +0 -1
- package/dist/mod.js.map +1 -1
- package/dist/otel.d.ts +1 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/pubsub/encoding.d.ts +5 -0
- package/dist/pubsub/encoding.d.ts.map +1 -0
- package/dist/pubsub/encoding.js +10 -2
- package/dist/pubsub/encoding.js.map +1 -1
- package/dist/pubsub/mod.d.ts +4 -3
- package/dist/pubsub/mod.d.ts.map +1 -0
- package/dist/pubsub/mod.js +2 -2
- package/dist/pubsub/mod.js.map +1 -1
- package/dist/pubsub/refs.d.ts +8 -0
- package/dist/pubsub/refs.d.ts.map +1 -0
- package/dist/pubsub/refs.js.map +1 -1
- package/dist/pubsub/subscription.d.ts +104 -10
- package/dist/pubsub/subscription.d.ts.map +1 -0
- package/dist/pubsub/subscription.js +98 -16
- package/dist/pubsub/subscription.js.map +1 -1
- package/dist/pubsub/topic.d.ts +87 -23
- package/dist/pubsub/topic.d.ts.map +1 -0
- package/dist/pubsub/topic.js +143 -40
- package/dist/pubsub/topic.js.map +1 -1
- package/dist/reqctx/mod.d.ts +191 -0
- package/dist/reqctx/mod.d.ts.map +1 -0
- package/dist/reqctx/mod.js +160 -0
- package/dist/reqctx/mod.js.map +1 -0
- package/dist/services/index.d.ts +145 -4
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +155 -8
- package/dist/services/index.js.map +1 -1
- package/dist/shutdown/mod.d.ts +1 -0
- package/dist/shutdown/mod.d.ts.map +1 -0
- package/dist/storage/objects/bucket.d.ts +312 -7
- package/dist/storage/objects/bucket.d.ts.map +1 -0
- package/dist/storage/objects/bucket.js +305 -15
- package/dist/storage/objects/bucket.js.map +1 -1
- package/dist/storage/objects/error.d.ts +10 -0
- package/dist/storage/objects/error.d.ts.map +1 -0
- package/dist/storage/objects/error.js +25 -1
- package/dist/storage/objects/error.js.map +1 -1
- package/dist/storage/objects/mod.d.ts +4 -3
- package/dist/storage/objects/mod.d.ts.map +1 -0
- package/dist/storage/objects/mod.js +1 -1
- package/dist/storage/objects/mod.js.map +1 -1
- package/dist/storage/objects/refs.d.ts +20 -4
- package/dist/storage/objects/refs.d.ts.map +1 -0
- package/dist/storage/objects/refs.js +6 -0
- package/dist/storage/objects/refs.js.map +1 -1
- package/dist/storage/postgres/database.d.ts +81 -25
- package/dist/storage/postgres/database.d.ts.map +1 -0
- package/dist/storage/postgres/database.js +125 -37
- package/dist/storage/postgres/database.js.map +1 -1
- package/dist/storage/postgres/errors.d.ts +120 -0
- package/dist/storage/postgres/errors.d.ts.map +1 -0
- package/dist/storage/postgres/errors.js +162 -0
- package/dist/storage/postgres/errors.js.map +1 -0
- package/dist/storage/postgres/interval.d.ts +44 -0
- package/dist/storage/postgres/interval.d.ts.map +1 -0
- package/dist/storage/postgres/interval.js +153 -0
- package/dist/storage/postgres/interval.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +4 -1
- package/dist/storage/postgres/mod.d.ts.map +1 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -1
- package/dist/storage/redis/bitfield.d.ts +67 -0
- package/dist/storage/redis/bitfield.d.ts.map +1 -0
- package/dist/storage/redis/bitfield.js +114 -0
- package/dist/storage/redis/bitfield.js.map +1 -0
- package/dist/storage/redis/command.d.ts +45 -0
- package/dist/storage/redis/command.d.ts.map +1 -0
- package/dist/storage/redis/command.js +78 -0
- package/dist/storage/redis/command.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +223 -139
- package/dist/storage/redis/commands.d.ts.map +1 -0
- package/dist/storage/redis/commands.js +128 -6
- package/dist/storage/redis/commands.js.map +1 -1
- package/dist/storage/redis/database.d.ts +56 -22
- package/dist/storage/redis/database.d.ts.map +1 -0
- package/dist/storage/redis/database.js +62 -0
- package/dist/storage/redis/database.js.map +1 -1
- package/dist/storage/redis/mod.d.ts +31 -4
- package/dist/storage/redis/mod.d.ts.map +1 -0
- package/dist/storage/redis/mod.js +26 -1
- package/dist/storage/redis/mod.js.map +1 -1
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.d.ts.map +1 -0
- package/dist/storage/redis/native.js +10 -0
- package/dist/storage/redis/native.js.map +1 -1
- package/dist/storage/redis/options.d.ts +1 -0
- package/dist/storage/redis/options.d.ts.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +2 -0
- package/dist/storage/redis/pubsub.d.ts.map +1 -0
- package/dist/storage/redis/reply.d.ts +34 -0
- package/dist/storage/redis/reply.d.ts.map +1 -0
- package/dist/storage/redis/reply.js +40 -1
- package/dist/storage/redis/reply.js.map +1 -1
- package/dist/storage/redis/scan.d.ts +1 -0
- package/dist/storage/redis/scan.d.ts.map +1 -0
- package/dist/storage/redis/script.d.ts +1 -0
- package/dist/storage/redis/script.d.ts.map +1 -0
- package/dist/storage/redis/target.d.ts +5 -3
- package/dist/storage/redis/target.d.ts.map +1 -0
- package/dist/storage/redis/target.js +39 -28
- package/dist/storage/redis/target.js.map +1 -1
- package/dist/storage/redis/wire.d.ts +7 -10
- package/dist/storage/redis/wire.d.ts.map +1 -0
- package/dist/storage/redis/wire.js +8 -32
- package/dist/storage/redis/wire.js.map +1 -1
- package/dist/testing/bun.d.ts +8 -0
- package/dist/testing/bun.d.ts.map +1 -0
- package/dist/testing/bun.js +13 -0
- package/dist/testing/bun.js.map +1 -0
- package/dist/testing/jest.d.ts +8 -0
- package/dist/testing/jest.d.ts.map +1 -0
- package/dist/testing/jest.js +11 -0
- package/dist/testing/jest.js.map +1 -0
- package/dist/testing/mod.d.ts +160 -0
- package/dist/testing/mod.d.ts.map +1 -0
- package/dist/testing/mod.js +227 -0
- package/dist/testing/mod.js.map +1 -0
- package/dist/testing/node.d.ts +9 -0
- package/dist/testing/node.d.ts.map +1 -0
- package/dist/testing/node.js +12 -0
- package/dist/testing/node.js.map +1 -0
- package/dist/testing/vitest.d.ts +8 -0
- package/dist/testing/vitest.d.ts.map +1 -0
- package/dist/testing/vitest.js +13 -0
- package/dist/testing/vitest.js.map +1 -0
- package/dist/tracing/mod.d.ts +19 -0
- package/dist/tracing/mod.d.ts.map +1 -0
- package/dist/tracing/mod.js +46 -0
- package/dist/tracing/mod.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/mod.d.ts +1 -0
- package/dist/types/mod.d.ts.map +1 -0
- package/gateway/errors.ts +172 -0
- package/gateway/gateway.e2e.test.ts +297 -0
- package/gateway/gateway.test.ts +891 -0
- package/gateway/match.ts +239 -0
- package/gateway/middleware.ts +134 -0
- package/gateway/mod.ts +210 -0
- package/gateway/request.ts +578 -0
- package/gateway/serve.ts +738 -0
- package/internal/clients/call.ts +389 -0
- package/internal/clients/grpc.test.ts +368 -0
- package/internal/clients/grpc.ts +485 -0
- package/internal/clients/mod.test.ts +464 -0
- package/internal/clients/mod.ts +337 -0
- package/internal/fetch.ts +276 -0
- package/internal/otel/index.ts +1 -7
- package/internal/otel/span.ts +6 -0
- package/internal/otel/tracer.test.ts +43 -15
- package/internal/otel/tracer.ts +12 -5
- package/internal/reqfields.ts +390 -0
- package/internal/runtime/mod.ts +41 -2
- package/internal/runtime/napi/napi.cjs +160 -16
- package/internal/runtime/napi/napi.d.cts +474 -29
- package/internal/runtime/shutdown.ts +11 -1
- package/internal/testing/runner.ts +10 -0
- package/internal/testing/scope.ts +273 -0
- package/metrics/mod.test.ts +13 -16
- package/metrics/mod.ts +57 -226
- package/mod.ts +0 -8
- package/otel.test.ts +13 -6
- package/package.json +67 -15
- package/pubsub/encoding.ts +11 -1
- package/pubsub/mod.ts +10 -3
- package/pubsub/pubsub.test.ts +232 -14
- package/pubsub/refs.ts +16 -0
- package/pubsub/subscription.ts +194 -16
- package/pubsub/topic.test.ts +26 -0
- package/pubsub/topic.ts +220 -25
- package/reqctx/mod.ts +284 -0
- package/reqctx/reqctx.test.ts +193 -0
- package/scripts/postprocess-docs.test.ts +48 -0
- package/services/index.test.ts +341 -1
- package/services/index.ts +183 -8
- package/storage/objects/bucket.test.ts +573 -0
- package/storage/objects/bucket.ts +649 -20
- package/storage/objects/error.ts +28 -1
- package/storage/objects/mod.ts +15 -0
- package/storage/objects/refs.ts +51 -3
- package/storage/postgres/database.integration.test.ts +275 -0
- package/storage/postgres/database.test.ts +193 -6
- package/storage/postgres/database.ts +202 -55
- package/storage/postgres/errors.ts +186 -0
- package/storage/postgres/interval.test.ts +72 -0
- package/storage/postgres/interval.ts +170 -0
- package/storage/postgres/mod.ts +4 -0
- package/storage/redis/bitfield.ts +162 -0
- package/storage/redis/command.ts +84 -0
- package/storage/redis/commands.ts +271 -21
- package/storage/redis/database.ts +77 -23
- package/storage/redis/mod.ts +38 -2
- package/storage/redis/native.ts +11 -0
- package/storage/redis/redis.test.ts +170 -10
- package/storage/redis/reply.ts +76 -1
- package/storage/redis/runtime.test.ts +15 -0
- package/storage/redis/target.ts +47 -44
- package/storage/redis/wire.ts +8 -32
- package/testing/bun.ts +13 -0
- package/testing/jest.ts +11 -0
- package/testing/mod.ts +337 -0
- package/testing/node.ts +12 -0
- package/testing/vitest.ts +13 -0
- package/tracing/mod.ts +82 -0
- package/tracing/native.test.ts +170 -0
- package/tracing/tracing.test.ts +317 -0
- package/dist/internal/otel/request_context.d.ts +0 -11
- package/dist/internal/otel/request_context.js +0 -20
- package/dist/internal/otel/request_context.js.map +0 -1
- package/dist/internal/reqtrack/mod.d.ts +0 -6
- package/dist/internal/reqtrack/mod.js +0 -28
- package/dist/internal/reqtrack/mod.js.map +0 -1
- package/dist/req_meta.d.ts +0 -47
- package/dist/req_meta.js +0 -26
- package/dist/req_meta.js.map +0 -1
- package/internal/otel/request_context.ts +0 -32
- package/internal/reqtrack/mod.ts +0 -39
- package/req_meta.ts +0 -75
package/services/index.ts
CHANGED
|
@@ -1,21 +1,196 @@
|
|
|
1
|
-
import {runtimeConfig} from '../internal/runtime/mod';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
2
|
+
* Calling the app's other services.
|
|
3
|
+
*
|
|
4
|
+
* {@link client} returns a client for one service, whose `fetch` takes the
|
|
5
|
+
* service's paths:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* import {client} from '@encore.dev/sdk/services';
|
|
9
|
+
*
|
|
10
|
+
* const billing = client('billing');
|
|
11
|
+
*
|
|
12
|
+
* export async function invoices(): Promise<Invoice[]> {
|
|
13
|
+
* const res = await billing.fetch('/invoices?limit=10');
|
|
14
|
+
* if (!res.ok) throw new Error(`billing: ${res.status}`);
|
|
15
|
+
* return res.json();
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Its calls, and those made with this module's {@link fetch}, carry the
|
|
20
|
+
* trace and Request Context (`@encore.dev/sdk/reqctx`) of the active context
|
|
21
|
+
* to the app's services, so the services they reach continue the request
|
|
22
|
+
* they are made for. A request to anything else carries no Request Context:
|
|
23
|
+
* who a request is made for is no business of a third party's. Pass
|
|
24
|
+
* {@link fetch} to a library that takes one, such as a generated client.
|
|
25
|
+
*
|
|
26
|
+
* A service that lists another in its `encore.service.json` `clients` gets
|
|
27
|
+
* generated clients for calling it in `encore_gen/clients`: a module per
|
|
28
|
+
* service, with a module per API the service declares. Each operation is a
|
|
29
|
+
* function of its module, so there is no client to make first:
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* import {catalog} from "./encore_gen/clients/index.js";
|
|
33
|
+
*
|
|
34
|
+
* const product = await catalog.openapi.getProduct("xyz");
|
|
35
|
+
* const users = await catalog.openapi.listUsers({limit: 10});
|
|
36
|
+
* const stuff = await catalog.grpc.stuffService.doStuff({});
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Importing the index loads every client of every service, cheaply: a gRPC
|
|
40
|
+
* client loads its packages only for its first call, so a service that
|
|
41
|
+
* makes no gRPC calls needs none of them. A deep import loads one alone:
|
|
42
|
+
* `import * as openapi from "./encore_gen/clients/catalog/openapi.js"`.
|
|
43
|
+
*
|
|
44
|
+
* A generated call that fails throws a {@link CallError}, whatever kind of
|
|
45
|
+
* API it calls, which `instanceof CallError` tells from any other error. Its
|
|
46
|
+
* code is the service's answer to the caller, not the caller's to its own
|
|
47
|
+
* clients: one an `@encore.dev/api` handler lets escape answers internal, as
|
|
48
|
+
* any error but an APIError does. A handler answers with a code of its own
|
|
49
|
+
* where it means one, and {@link unlessNotFound} makes a `not_found` failure
|
|
50
|
+
* `undefined`, for a call for something that may not be there:
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* import {APIError} from "@encore.dev/api";
|
|
54
|
+
* import {CallError, unlessNotFound} from "@encore.dev/sdk/services";
|
|
55
|
+
*
|
|
56
|
+
* try {
|
|
57
|
+
* await catalog.openapi.deleteProduct("xyz");
|
|
58
|
+
* } catch (err) {
|
|
59
|
+
* if (err instanceof CallError && err.code === "not_found") {
|
|
60
|
+
* throw APIError.notFound("no such product");
|
|
61
|
+
* }
|
|
62
|
+
* throw err; // internal
|
|
63
|
+
* }
|
|
64
|
+
*
|
|
65
|
+
* const product = await unlessNotFound(catalog.openapi.getProduct("xyz"));
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* Every generated call also takes {@link CallOptions}: a timeout, in
|
|
69
|
+
* milliseconds, headers and an `AbortSignal` of its own, which go over its
|
|
70
|
+
* client's. A module's `Client` holds {@link ClientOptions} for every call
|
|
71
|
+
* made with it:
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* const fresh = await catalog.openapi.getProduct("xyz", {timeout: 2_000});
|
|
75
|
+
* const slow = new catalog.openapi.Client({timeout: 10_000});
|
|
76
|
+
* const mocked = new catalog.openapi.Client({baseUrl: mockServer.url});
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* The generated clients send through this module's {@link fetch}, so their
|
|
80
|
+
* calls carry the trace and Request Context as {@link client}'s do. A
|
|
81
|
+
* client holds only its settings, so one made for a single call costs
|
|
82
|
+
* nothing, and there is nothing to close.
|
|
83
|
+
*
|
|
84
|
+
* Looking up a service's URL, or making a client for it, makes the calling
|
|
85
|
+
* service depend on it. Encore reads the dependency from the code, as it
|
|
86
|
+
* reads which resources a service uses: so the name must be a string
|
|
87
|
+
* literal, the function must be called directly rather than stored or passed
|
|
88
|
+
* around, and the call must be in a service.
|
|
7
89
|
*
|
|
8
90
|
* No service runs under `encore test`. Every service still has a base URL
|
|
9
91
|
* there, but a call to it is answered with `501 Not Implemented` and an error
|
|
10
92
|
* saying which service and path the test called: mock the call in the test
|
|
11
93
|
* instead.
|
|
94
|
+
*
|
|
95
|
+
* @module
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
import {fetch, recordOrigin} from '../internal/fetch';
|
|
99
|
+
import {runtimeConfig} from '../internal/runtime/mod';
|
|
100
|
+
|
|
101
|
+
export {fetch, wrapFetch} from '../internal/fetch';
|
|
102
|
+
export {
|
|
103
|
+
CallError,
|
|
104
|
+
httpStatus,
|
|
105
|
+
isErrCode,
|
|
106
|
+
unlessNotFound,
|
|
107
|
+
type CallOptions,
|
|
108
|
+
type ClientOptions,
|
|
109
|
+
type ErrCode,
|
|
110
|
+
type GraphQLError,
|
|
111
|
+
} from '../internal/clients/call';
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The base URL the calling service reaches `service` at, without a trailing
|
|
115
|
+
* slash: a call's path goes after it, `` `${baseUrl("orders")}/orders/1` ``.
|
|
116
|
+
*
|
|
117
|
+
* Throws if the service is unknown to this process: the app has no service of
|
|
118
|
+
* that name, or the process was started without the app's service discovery.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* import {baseUrl} from "@encore.dev/sdk/services";
|
|
122
|
+
* const res = await fetch(`${baseUrl("orders")}/orders/1`);
|
|
123
|
+
*/
|
|
124
|
+
export function baseUrl(service: string): string {
|
|
125
|
+
return lookup(service).replace(/\/+$/, '');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The base URL of `service` as a `URL`, ending in `/`. Resolve relative paths
|
|
130
|
+
* against it: `new URL("orders/1", serviceUrl("orders"))`. Throws if the
|
|
131
|
+
* service is unknown to this process.
|
|
132
|
+
*
|
|
133
|
+
* Looking it up makes the calling service depend on `service`, as
|
|
134
|
+
* {@link baseUrl} does, under the same rules.
|
|
135
|
+
*
|
|
136
|
+
* @deprecated Use {@link baseUrl}, which returns the URL as a string without a
|
|
137
|
+
* trailing slash, as the other Encore SDKs do.
|
|
12
138
|
*/
|
|
13
139
|
export function serviceUrl(service: string): URL {
|
|
140
|
+
const u = new URL(lookup(service));
|
|
141
|
+
if (!u.pathname.endsWith('/')) u.pathname += '/';
|
|
142
|
+
return u;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The base URL of `service` as the runtime config gives it, whose origin
|
|
146
|
+
* it records as one of the app's services'. */
|
|
147
|
+
function lookup(service: string): string {
|
|
14
148
|
const base = runtimeConfig().services[service];
|
|
15
149
|
if (base === undefined) {
|
|
16
150
|
throw new Error(`encore: unknown service ${JSON.stringify(service)}`);
|
|
17
151
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
152
|
+
recordOrigin(base);
|
|
153
|
+
return base;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** A client for one of the app's services. */
|
|
157
|
+
export interface ServiceClient {
|
|
158
|
+
/** The service's base URL, without a trailing slash, as {@link baseUrl}
|
|
159
|
+
* returns it. */
|
|
160
|
+
readonly baseUrl: string;
|
|
161
|
+
/**
|
|
162
|
+
* Calls the service, as this module's {@link fetch} does. `path` is the
|
|
163
|
+
* service's: it is joined to the base URL with a single `/`, so
|
|
164
|
+
* `'/invoices'` and `'invoices'` are the same call. An absolute URL is
|
|
165
|
+
* used as it is.
|
|
166
|
+
*/
|
|
167
|
+
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A client for `service`, for service-to-service calls: its `fetch` takes the
|
|
172
|
+
* service's paths, and carries the trace and Request Context of the active
|
|
173
|
+
* context.
|
|
174
|
+
*
|
|
175
|
+
* Like {@link baseUrl}, it makes the calling service depend on `service`, so
|
|
176
|
+
* the name must be a string literal and the call made directly, in a service.
|
|
177
|
+
* Throws if the service is unknown to this process.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* import {client} from "@encore.dev/sdk/services";
|
|
181
|
+
* const orders = client("orders");
|
|
182
|
+
* const res = await orders.fetch("/orders/1");
|
|
183
|
+
*/
|
|
184
|
+
export function client(service: string): ServiceClient {
|
|
185
|
+
const base = baseUrl(service);
|
|
186
|
+
return {
|
|
187
|
+
baseUrl: base,
|
|
188
|
+
fetch: (path, init) => fetch(join(base, path), init),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** `path` under `base`, with one `/` between; an absolute URL as it is. */
|
|
193
|
+
function join(base: string, path: string): string {
|
|
194
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(path)) return path;
|
|
195
|
+
return `${base}/${path.replace(/^\/+/, '')}`;
|
|
21
196
|
}
|