gRPC schema
Fetch the protobuf schema of the Synthient gRPC API.
The @synthient/synthient/grpcschema entry point fetches the protobuf schema of the Synthient gRPC API using gRPC server reflection. It needs the optional peer dependencies @grpc/grpc-js and @bufbuild/protobuf, and importing it without them fails:
npm install @grpc/grpc-js @bufbuild/protobuf
fetchSchema
Connects to a Synthient gRPC endpoint and uses server reflection to fetch the protobuf files that define options.symbols, or every listed service when there are none, along with all of their imports.
function fetchSchema(options?: FetchSchemaOptions): Promise<Schema>;
import { SchemaError, explain, fetchSchema } from "@synthient/synthient/grpcschema";
try {
const schema = await fetchSchema();
for (const file of schema.descriptorSet.file) {
console.log(file.name, file.package);
}
} catch (error) {
const explanation = explain(error);
if (error instanceof SchemaError && explanation !== "") {
throw new Error(`${explanation} ${error.message}`, { cause: error });
}
throw error;
}
- Errors: Errors from the gRPC call or the reflection responses reject with a
SchemaError, invalid options with aTypeErrororRangeError, and a canceledsignalwith its reason.
FetchSchemaOptions
With no options, fetchSchema fetches every service from DEFAULT_ENDPOINT over TLS, sending the key from SYNTHIENT_API_KEY if it is set.
- Name
apiKey?- Type
- string | undefined
- Description
Sent as
x-api-keymetadata when not empty. Defaults to theSYNTHIENT_API_KEYenvironment variable.
- Name
endpoint?- Type
- string | undefined
- Description
The server as
host:port, a bare host (port 443 is assumed), or agrpc://orhttps://URL. Defaults toDEFAULT_ENDPOINT.
- Name
plaintext?- Type
- boolean | undefined
- Description
Disables TLS, for local development servers.
- Name
symbols?- Type
- Iterable<string> | undefined
- Description
Fully qualified service or message names to resolve. Empty resolves every listed service except the
grpc.reflectionservices.
- Name
signal?- Type
- AbortSignal | undefined
- Description
Cancels the fetch, which rejects with the signal's reason.
- Name
timeout?- Type
- number | undefined
- Description
Milliseconds allowed for the whole fetch. Defaults to
30_000. It must be an integer from 1 to 2147483647.
Schema
The protobuf schema returned by fetchSchema.
- Name
endpoint- Type
- string
- Description
The normalized address that was queried.
- Name
symbols- Type
- string[]
- Description
The service or message names that were resolved.
- Name
descriptorSet- Type
- FileDescriptorSet
- Description
The resolved files in dependency order, each file after the files it imports.
FileDescriptorSetcomes from@bufbuild/protobuf/wkt.
SchemaError
Fetching the schema failed. explain describes common causes. It extends SynthientError, and its name is "SchemaError".
- Name
code- Type
- number | undefined
- Description
The gRPC status code, as in
statusfrom@grpc/grpc-js, orundefinedwhen the failure was not a gRPC status.
constructor(message: string, code?: number, options?: ErrorOptions);
explain
Returns a human-readable explanation of a common fetchSchema error, or "" if it has none, including for any error that is not a SchemaError.
function explain(error: unknown): string;
| gRPC status | Explanation |
|---|---|
UNAUTHENTICATED | Missing or invalid API key. |
PERMISSION_DENIED | This API key does not have permission to inspect gRPC schemas. |
NOT_FOUND | No protobuf schema found for the requested symbol. Fetch without symbols to list the available services. |
UNIMPLEMENTED | The gRPC endpoint does not expose server reflection. |
UNAVAILABLE | Could not reach the gRPC endpoint. Check the address, network access, and TLS settings. |
DEADLINE_EXCEEDED | Timed out reading protobuf schemas from gRPC reflection. Use a longer timeout. |
normalizeEndpoint
Converts raw to host:port form and returns it with the host name used for TLS.
function normalizeEndpoint(raw: string): {
endpoint: string;
host: string;
};
- Input: It accepts
host:port, a bare host (port 443 is assumed), andgrpc://orhttps://URLs. An emptyrawusesDEFAULT_ENDPOINT.
The address of the Synthient gRPC API.
const DEFAULT_ENDPOINT = "grpc.synthient.com:443";