Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Four paths, and `package.json`'s `exports` lists no others (spec 000 §5a):

A new export goes on the lowest of these that serves it; the root grows
only for something a client user writes or reads. On the client, reads are
`client.query` / `queryPage` / `get`, and writes are `client.send`,
`client.query` / `queryPage` / `get`, `client.search` across asset types
and `client.deleted`, and writes are `client.send`,
`client.preview` and `client.sendGuarded` (a guarded write compares each
asset's `Moment`; what matched nothing is a conflict, never a success:
spec 000 §6).
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

Nothing in the package changes.

- `examples/11-axios-transport.ts`: a `Transport` over an axios instance
and the adapter that runs `transportConformance` against it. axios is a
dev dependency of the repository for that example alone; the package
still depends on nothing. (a browser-app consumer.)

## 1.0.0-alpha.9

### Breaking
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const stories = await client.query({ from: "Story", select: ["Name", "Estimate"]
// stories[0].Estimate is a number | null; stories[0].Nmae does not compile
```

Runnable examples, one per task (a first read, a write, a guarded write, a batch with an undo, errors, attachments, testing your own code, a transport of your own, a browser session that expires), are in [`examples/`](examples/README.md).
Runnable examples, one per task (a first read, a write, a guarded write, a batch with an undo, errors, attachments, testing your own code, a transport of your own, a browser session that expires, searching, a transport over axios), are in [`examples/`](examples/README.md).

## Working on the SDK

Expand Down
129 changes: 129 additions & 0 deletions examples/11-axios-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* A Transport built on axios, and the conformance suite run against it.
* For an app that already talks HTTP through an axios instance (its
* interceptors, its cancellation) and wants the SDK to use the same one.
*
* Two parts, each to copy: `createAxiosTransport`, the Transport; and
* `adapterFor`, which lets the suite stand in for the network, so the
* checks exercise axios itself rather than a mock of it. In a test file
* the last lines become `for (const check of checks) it(check.name, check.run)`.
*
* Needs no instance and no credentials. axios is this repository's dev
* dependency for this example only; the SDK itself depends on nothing.
*
* node examples/11-axios-transport.ts
*/
import axios, { AxiosError, AxiosHeaders, CanceledError, type AxiosAdapter, type AxiosInstance, type InternalAxiosRequestConfig, type RawAxiosHeaders } from "axios";
import { ApiError, apiErrorFromResponse } from "@versionone/agility-sdk";
import type { RequestOptions, Transport } from "@versionone/agility-sdk";
import { CONFORMANCE_BASE_URL, transportConformance, type ConformanceServer } from "@versionone/agility-sdk/testing";

const aborted = (cause?: unknown) => new ApiError("Request aborted", 0, "ABORTED", { cause });
const isJson = (contentType: string) => /\bjson\b/i.test(contentType);

/** What to send for a body, and the content type it implies: the SDK's rule, not axios's own guess. */
function bodyOf(options: RequestOptions): { data: unknown; type?: string } {
const { body, contentType } = options;
if (body === undefined || body === null) return { data: undefined };
if (typeof FormData !== "undefined" && body instanceof FormData) return { data: body }; // the runtime writes the boundary
if (typeof Blob !== "undefined" && body instanceof Blob) return { data: body, type: contentType ?? (body.type || "application/octet-stream") };
if (body instanceof Uint8Array || body instanceof ArrayBuffer) return { data: body, type: contentType ?? "application/octet-stream" };
if (typeof body === "string") return { data: body, type: contentType ?? "text/plain" };
return { data: JSON.stringify(body), type: contentType ?? "application/json" };
}

/** A response body as the SDK reads a refusal's: parsed when it is JSON, else the text. */
function readable(data: unknown, contentType: string): unknown {
const text = typeof data === "string" ? data : data instanceof ArrayBuffer ? new TextDecoder().decode(data) : data;
if (typeof text !== "string" || !isJson(contentType)) return text;
try { return JSON.parse(text); } catch { return text; }
}

/** The SDK's Transport over an axios instance whose `baseURL` is the Agility instance. */
export function createAxiosTransport(http: AxiosInstance): Transport {
return {
async request<T>(options: RequestOptions): Promise<T> {
// Nothing is sent for a request that was cancelled before it began.
if (options.signal?.aborted) throw aborted(options.signal.reason);
const { data, type } = bodyOf(options);
const bytes = options.responseType === "bytes";
let response;
try {
response = await http.request({
method: options.method,
url: options.path.replace(/^\/+/, ""), // relative, so it stays under baseURL's path
params: Object.fromEntries(Object.entries(options.query ?? {}).filter(([, value]) => value !== undefined && value !== null)),
headers: { ...options.headers, ...(type ? { "Content-Type": type } : {}) },
data,
signal: options.signal,
responseType: bytes ? "arraybuffer" : "text",
transformRequest: [(body) => body], // already in the shape to send
transformResponse: [(body) => body], // read below, by the SDK's rules
validateStatus: (status) => (status >= 200 && status < 300) || (status === 304 && options.acceptNotModified === true)
});
} catch (error) {
if (axios.isCancel(error) || options.signal?.aborted) throw aborted(error);
if (error instanceof AxiosError && error.response) {
// A refusal: the same error, word for word, as the SDK's own transport throws.
throw apiErrorFromResponse(error.response.status, readable(error.response.data, String(error.response.headers["content-type"] ?? "")));
}
// Timing is this transport's own business today: the client's `timeoutMs` binds only the SDK's built-in
// transport, so the axios instance needs its own `timeout`, and its timeout must be reported as TIMEOUT
// here for the client's resend rule to recognise it. Were the client to enforce `timeoutMs` above every
// transport (it would hand over a signal that fires when time is up), neither would be needed: honouring
// `signal`, as above, would be all a transport owes, and these two lines could go.
const timedOut = error instanceof AxiosError && (error.code === AxiosError.ECONNABORTED || error.code === AxiosError.ETIMEDOUT);
throw new ApiError(error instanceof Error ? error.message : String(error), 0, timedOut ? "TIMEOUT" : "NETWORK_ERROR", { cause: error });
}
const headers = AxiosHeaders.from(response.headers as RawAxiosHeaders);
options.onResponse?.({ status: response.status, headers: { get: (name) => { const value = headers.get(name); return value == null ? null : String(value); } } });
if (response.status === 304) return undefined as T;
if (bytes) return new Uint8Array(response.data as ArrayBuffer) as T;
const text = String(response.data ?? "");
const wanted = options.responseType ?? "auto";
if (wanted === "text" || (wanted === "auto" && !isJson(String(headers.get("content-type") ?? "")))) return text as T;
if (text === "") return undefined as T; // an empty body is no value, not a parse error
try { return JSON.parse(text) as T; } catch { throw new ApiError("Response is not JSON", response.status, "BAD_JSON", { body: text.slice(0, 500) }); }
}
};
}

/** axios as it is in an app, except that its network is the suite's server. */
const adapterFor = (server: ConformanceServer): AxiosAdapter => async (config: InternalAxiosRequestConfig) => {
const signal = config.signal as AbortSignal | undefined;
if (signal?.aborted) throw new CanceledError(undefined, undefined, config);
const headers: Record<string, string> = {};
for (const [name, value] of Object.entries(AxiosHeaders.from(config.headers).toJSON())) {
if (value !== undefined && value !== null) headers[name.toLowerCase()] = String(value);
}
const answer = await server({
method: (config.method ?? "get").toUpperCase(),
url: axios.getUri(config), // baseURL + url + params, as axios would send it
headers,
body: config.data, // after transformRequest: what axios would put on the wire
signal
});
if (signal?.aborted) throw new CanceledError(undefined, undefined, config);
if (answer === "network-error") throw new AxiosError("Network Error", AxiosError.ERR_NETWORK, config);
const body = answer.body ?? "";
const data = config.responseType === "arraybuffer"
? (typeof body === "string" ? new TextEncoder().encode(body).buffer : body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength))
: (typeof body === "string" ? body : new TextDecoder().decode(body));
const response = { data, status: answer.status, statusText: "", headers: AxiosHeaders.from(answer.headers ?? {}), config, request: {} };
// A custom adapter settles the response itself. Without this, a 304 and a refusal would pass the suite for the wrong reason.
if (!config.validateStatus || config.validateStatus(answer.status)) return response;
throw new AxiosError(`Request failed with status code ${answer.status}`, AxiosError.ERR_BAD_RESPONSE, config, {}, response);
};

const checks = transportConformance((server) => createAxiosTransport(axios.create({ baseURL: CONFORMANCE_BASE_URL, adapter: adapterFor(server) })));
let broken = 0;
for (const check of checks) {
try {
await check.run();
} catch (error) {
broken += 1;
console.log(`BROKEN: ${(error as Error).message}`);
}
}
console.log(`${checks.length - broken} of ${checks.length} conformance checks pass for the axios transport`);
if (broken) process.exitCode = 1;
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Examples

Ten short programs, one per task, each runnable as it stands. Read them
Eleven short programs, one per task, each runnable as it stands. Read them
in order; each says at the top what it does.

| File | Shows | Writes? |
Expand All @@ -15,6 +15,7 @@ in order; each says at the top what it does.
| `08-custom-transport.ts` | A `Transport` of your own (a timed wrapper), checked with `transportConformance` | no; a live read only if an instance is set |
| `09-browser-session.ts` | Cookie auth for a browser app: `credentials`, `onUnauthorized`, and when `"retry-write"` is yours to answer | no instance at all (a stand-in server) |
| `10-search.ts` | `client.search` across asset types; `find` on an ordinary read within one family | no |
| `11-axios-transport.ts` | A `Transport` over an axios instance, and an axios adapter that lets `transportConformance` stand in for the network | no instance at all |

## Running one

Expand Down
Loading