Skip to content

Commit

Permalink
Remove Params/Headers types
Browse files Browse the repository at this point in the history
  • Loading branch information
pluma4345 committed Apr 2, 2024
1 parent af6f686 commit af50c2b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 67 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ for upgrading your code to arangojs v9.

For more information, see [the Node.js release schedule](https://nodejs.dev/en/about/releases/).

- Removed `Params` and `Headers` types

These can mostly be replaced with the native `URLSearchParams` and `Headers`
types but most public methods still accept the equivalent `Record` types for
convenience.

### Changed

- Replaced request logic with native `fetch` API
Expand Down
34 changes: 9 additions & 25 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
RequestFunction,
} from "./lib/request";
import { joinPath } from "./lib/joinPath";
import { mergeHeaders } from "./lib/mergeHeaders";

const MIME_JSON = /\/(json|javascript)(\W|$)/;
const LEADER_ENDPOINT_HEADER = "x-arango-endpoint";
Expand All @@ -48,20 +49,6 @@ const LEADER_ENDPOINT_HEADER = "x-arango-endpoint";
*/
export type LoadBalancingStrategy = "NONE" | "ROUND_ROBIN" | "ONE_RANDOM";

/**
* An arbitrary object with string values representing HTTP headers and their
* values.
*
* Header names should always be lowercase.
*/
export type Headers = Record<string, string>;

/**
* An arbitrary object with scalar values representing query string parameters
* and their values.
*/
export type Params = Record<string, any>;

/**
* Generic properties shared by all ArangoDB HTTP API responses.
*/
Expand Down Expand Up @@ -177,7 +164,7 @@ export type RequestOptions = {
* HTTP headers to pass along with this request in addition to the default
* headers generated by arangojs.
*/
headers?: Headers | globalThis.Headers;
headers?: Headers | Record<string, string>;
/**
* Time in milliseconds after which arangojs will abort the request if the
* socket has not already timed out.
Expand All @@ -196,7 +183,7 @@ export type RequestOptions = {
/**
* URL parameters to pass as part of the query string.
*/
qs?: Params | URLSearchParams;
qs?: URLSearchParams | Record<string, any>;
};

/**
Expand All @@ -217,7 +204,7 @@ type Task = {
timeout?: number;
pathname: string;
qs?: URLSearchParams;
headers: globalThis.Headers;
headers: Headers;
body: any;
};
};
Expand Down Expand Up @@ -391,7 +378,7 @@ export type Config = {
* using {@link database.Database#useBasicAuth}, {@link database.Database#useBearerAuth} or
* the `auth` configuration option.
*/
headers?: Headers | globalThis.Headers;
headers?: Headers | Record<string, string>;
/**
* If set to `true`, arangojs will generate stack traces every time a request
* is initiated and augment the stack traces of any errors it generates.
Expand Down Expand Up @@ -431,7 +418,7 @@ export function isArangoConnection(connection: any): connection is Connection {
export class Connection {
protected _activeTasks: number = 0;
protected _arangoVersion: number = 31100;
protected _headers: globalThis.Headers;
protected _headers: Headers;
protected _loadBalancingStrategy: LoadBalancingStrategy;
protected _maxRetries: number | false;
protected _taskPoolSize: number;
Expand Down Expand Up @@ -475,7 +462,7 @@ export class Connection {
beforeRequest: config.beforeRequest,
afterResponse: config.afterResponse,
};
this._headers = new globalThis.Headers(config.headers);
this._headers = new Headers(config.headers);
this._headers.set("x-arango-version", String(this._arangoVersion));
this._headers.set(
"x-arango-driver",
Expand Down Expand Up @@ -898,10 +885,7 @@ export class Connection {
transform?: (res: ArangojsResponse) => T
): Promise<T> {
return new Promise((resolve, reject) => {
const headers = new globalThis.Headers([
...this._headers.entries(),
...(requestHeaders ? Object.entries(requestHeaders) : []),
]);
const headers = mergeHeaders(this._headers, requestHeaders ?? {});

if (body && !(body instanceof FormData)) {
let contentType;
Expand Down Expand Up @@ -935,7 +919,7 @@ export class Connection {
(params instanceof URLSearchParams
? params
: new URLSearchParams(params)),
headers: headers,
headers,
timeout,
method,
expectBinary,
Expand Down
6 changes: 3 additions & 3 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
ArangoApiResponse,
Config,
Connection,
Headers,
RequestOptions,
} from "./connection";
import { ArrayCursor, BatchedArrayCursor } from "./cursor";
Expand Down Expand Up @@ -1910,7 +1909,7 @@ export class Database {
* // with JSON request body '{"username": "admin", "password": "hunter2"}'
* ```
*/
route(path?: string, headers?: Headers): Route {
route(path?: string, headers?: Headers | Record<string, string>): Route {
return new Route(this, path, headers);
}

Expand Down Expand Up @@ -2006,7 +2005,8 @@ export class Database {
this._trapRequest = undefined;
return new Promise<T>(async (resolveRequest, rejectRequest) => {
const options = { ...opts };
options.headers = { ...options.headers, "x-arango-async": "store" };
options.headers = new Headers(options.headers);
options.headers.set("x-arango-async", "store");
let jobRes: ArangojsResponse;
try {
jobRes = await this._connection.request({ basePath, ...options });
Expand Down
17 changes: 8 additions & 9 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
EdgeCollection,
TraversalOptions,
} from "./collection";
import { Headers } from "./connection";
import { Database } from "./database";
import {
Document,
Expand Down Expand Up @@ -567,7 +566,7 @@ export class GraphVertexCollection<T extends Record<string, any> = any>
rev,
...qs
} = options;
const headers: Headers = {};
const headers: Record<string, string> = {};
if (rev) headers["if-match"] = rev;
const result = this._db.request(
{
Expand Down Expand Up @@ -664,7 +663,7 @@ export class GraphVertexCollection<T extends Record<string, any> = any>
options = { rev: options };
}
const { rev, ...qs } = options;
const headers: Headers = {};
const headers: Record<string, string> = {};
if (rev) headers["if-match"] = rev;
return this._db.request(
{
Expand Down Expand Up @@ -717,7 +716,7 @@ export class GraphVertexCollection<T extends Record<string, any> = any>
if (typeof options === "string") {
options = { rev: options };
}
const headers: Headers = {};
const headers: Record<string, string> = {};
const { rev, ...qs } = options;
if (rev) headers["if-match"] = rev;
return this._db.request(
Expand Down Expand Up @@ -772,7 +771,7 @@ export class GraphVertexCollection<T extends Record<string, any> = any>
if (typeof options === "string") {
options = { rev: options };
}
const headers: Headers = {};
const headers: Record<string, string> = {};
const { rev, ...qs } = options;
if (rev) headers["if-match"] = rev;
return this._db.request(
Expand Down Expand Up @@ -968,7 +967,7 @@ export class GraphEdgeCollection<T extends Record<string, any> = any>
rev,
...qs
} = options;
const headers: Headers = {};
const headers: Record<string, string> = {};
if (rev) headers["if-match"] = rev;
const result = this._db.request(
{
Expand Down Expand Up @@ -1071,7 +1070,7 @@ export class GraphEdgeCollection<T extends Record<string, any> = any>
options = { rev: options };
}
const { rev, ...qs } = options;
const headers: Headers = {};
const headers: Record<string, string> = {};
if (rev) headers["if-match"] = rev;
return this._db.request(
{
Expand Down Expand Up @@ -1133,7 +1132,7 @@ export class GraphEdgeCollection<T extends Record<string, any> = any>
options = { rev: options };
}
const { rev, ...qs } = options;
const headers: Headers = {};
const headers: Record<string, string> = {};
if (rev) headers["if-match"] = rev;
return this._db.request(
{
Expand Down Expand Up @@ -1180,7 +1179,7 @@ export class GraphEdgeCollection<T extends Record<string, any> = any>
options = { rev: options };
}
const { rev, ...qs } = options;
const headers: Headers = {};
const headers: Record<string, string> = {};
if (rev) headers["if-match"] = rev;
return this._db.request(
{
Expand Down
20 changes: 20 additions & 0 deletions src/lib/mergeHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Utility function for merging headers.
*
* @packageDocumentation
* @internal
*/

/**
* @internal
*/
export function mergeHeaders(
base: Headers,
extra: Headers | Record<string, string> | undefined
) {
if (!extra) return base;
return new Headers([
...base,
...(extra instanceof Headers ? extra : Object.entries(extra)),
]);
}
Loading

0 comments on commit af50c2b

Please sign in to comment.