Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Expose client.endpoints attribute (fixes #641) #742

Merged
merged 7 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 21 additions & 11 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
cleanUndefinedProperties,
} from "./utils";
import HTTP, { HttpResponse } from "./http";
import endpoint from "./endpoint";
import Endpoints from "./endpoints";
import * as requests from "./requests";
import { aggregate, AggregateResponse } from "./batch";
import Bucket from "./bucket";
import { capable } from "./utils";
import { addEndpointOptions, capable } from "./utils";
import {
HelloResponse,
KintoRequest,
Expand Down Expand Up @@ -90,6 +90,7 @@ export default class KintoClientBase {
public serverInfo: HelloResponse | null;
public events?: Emitter;
public http: HTTP;
public endpoints: typeof Endpoints;
private _remote!: string;
private _version!: string;

Expand Down Expand Up @@ -141,6 +142,8 @@ export default class KintoClientBase {
*/
this.events = options.events;

this.endpoints = Endpoints;

const { requestMode, timeout } = options;
/**
* The HTTP instance.
Expand Down Expand Up @@ -311,7 +314,7 @@ export default class KintoClientBase {
headers?: Record<string, string>;
} = {}
): Promise<HelloResponse> {
const path = this.remote + endpoint.root();
const path = this.remote + Endpoints.root();
const { json } = await this.http.request<HelloResponse>(
path,
{ headers: this._getHeaders(options) },
Expand Down Expand Up @@ -450,7 +453,7 @@ export default class KintoClientBase {
// FIXME: is this really necessary, since it's also present in
// the "defaults"?
headers,
path: endpoint.batch(),
path: Endpoints.batch(),
method: "POST",
body: {
defaults: { headers },
Expand Down Expand Up @@ -534,7 +537,13 @@ export default class KintoClientBase {
*/
async execute<T>(
request: KintoRequest,
options: { raw?: boolean; stringify?: boolean; retry?: number } = {}
options: {
raw?: boolean;
stringify?: boolean;
retry?: number;
query?: { [key: string]: string };
fields?: string[];
} = {}
): Promise<T | HttpResponse<T>> {
const { raw = false, stringify = true } = options;
// If we're within a batch, add the request to the stack to send at once.
Expand All @@ -551,8 +560,9 @@ export default class KintoClientBase {
? ({ status: 0, json: msg, headers: new Headers() } as HttpResponse<T>)
: msg;
}
const uri = this.remote + addEndpointOptions(request.path, options);
const result = await this.http.request<T>(
this.remote + request.path,
uri,
cleanUndefinedProperties({
// Limit requests to only those parts that would be allowed in
// a batch request -- don't pass through other fancy fetch()
Expand Down Expand Up @@ -720,7 +730,7 @@ export default class KintoClientBase {
headers?: Record<string, string>;
} = {}
): Promise<PaginationResult<PermissionData>> {
const path = endpoint.permissions();
const path = Endpoints.permissions();
// Ensure the default sort parameter is something that exists in permissions
// entries, as `last_modified` doesn't; here, we pick "id".
const paginationOptions = { sort: "id", ...options };
Expand Down Expand Up @@ -752,7 +762,7 @@ export default class KintoClientBase {
since?: string;
} = {}
): Promise<PaginationResult<KintoObject>> {
const path = endpoint.bucket();
const path = Endpoints.bucket();
return this.paginatedList<KintoObject>(path, options, {
headers: this._getHeaders(options),
retry: this._getRetry(options),
Expand Down Expand Up @@ -783,7 +793,7 @@ export default class KintoClientBase {
): Promise<KintoResponse<T>> {
const { data, permissions } = options;
const _data = { ...data, id: id ? id : undefined };
const path = _data.id ? endpoint.bucket(_data.id) : endpoint.bucket();
const path = _data.id ? Endpoints.bucket(_data.id) : Endpoints.bucket();
return (
this.execute<KintoResponse<T>>(
requests.createRequest(
Expand Down Expand Up @@ -825,7 +835,7 @@ export default class KintoClientBase {
if (!bucketObj.id) {
throw new Error("A bucket id is required.");
}
const path = endpoint.bucket(bucketObj.id);
const path = Endpoints.bucket(bucketObj.id);
const { last_modified } = { ...bucketObj, ...options };
return (
this.execute<KintoResponse<{ deleted: boolean }>>(
Expand Down Expand Up @@ -858,7 +868,7 @@ export default class KintoClientBase {
last_modified?: number;
} = {}
): Promise<KintoResponse<{ deleted: boolean }>> {
const path = endpoint.bucket();
const path = Endpoints.bucket();
return (
this.execute<KintoResponse<{ deleted: boolean }>>(
requests.deleteRequest(path, {
Expand Down
47 changes: 26 additions & 21 deletions src/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { toDataBody, isObject, capable, addEndpointOptions } from "./utils";
import { toDataBody, isObject, capable } from "./utils";
import Collection from "./collection";
import * as requests from "./requests";
import endpoint from "./endpoint";
import KintoClientBase, { PaginatedListParams, PaginationResult } from "./base";
import {
KintoRequest,
Expand Down Expand Up @@ -29,6 +28,7 @@ export interface BucketOptions {
export default class Bucket {
private client: KintoClientBase;
public name: string;
private _endpoints: any;
private _retry: number;
private _safe: boolean;
private _headers: Record<string, string>;
Expand Down Expand Up @@ -57,6 +57,9 @@ export default class Bucket {
* @type {String}
*/
this.name = name;

this._endpoints = client.endpoints;

/**
* @ignore
*/
Expand Down Expand Up @@ -145,7 +148,7 @@ export default class Bucket {
retry?: number;
} = {}
): Promise<string | null> {
const path = endpoint.collection(this.name);
const path = this._endpoints.collection(this.name);
const request: KintoRequest = {
headers: this._getHeaders(options),
path,
Expand Down Expand Up @@ -174,7 +177,7 @@ export default class Bucket {
retry?: number;
} = {}
): Promise<string | null> {
const path = endpoint.group(this.name);
const path = this._endpoints.group(this.name);
const request: KintoRequest = {
headers: this._getHeaders(options),
path,
Expand Down Expand Up @@ -210,15 +213,16 @@ export default class Bucket {
retry?: number;
} = {}
): Promise<T> {
let path = endpoint.bucket(this.name);
path = addEndpointOptions(path, options);
const path = this._endpoints.bucket(this.name);
const request = {
headers: this._getHeaders(options),
path,
};
const { data } =
(await this.client.execute(request, {
retry: this._getRetry(options),
query: options.query,
fields: options.fields,
})) as { data: T };
return data;
}
Expand Down Expand Up @@ -259,7 +263,7 @@ export default class Bucket {
delete bucket.id;
}

const path = endpoint.bucket(bucketId);
const path = this._endpoints.bucket(bucketId);
const { patch, permissions } = options;
const { last_modified } = { ...data, ...options };
const request = requests.updateRequest(
Expand Down Expand Up @@ -295,7 +299,7 @@ export default class Bucket {
retry?: number;
} = {}
): Promise<PaginationResult<HistoryEntry<T>>> {
const path = endpoint.history(this.name);
const path = this._endpoints.history(this.name);
return this.client.paginatedList<HistoryEntry<T>>(path, options, {
headers: this._getHeaders(options),
retry: this._getRetry(options),
Expand All @@ -322,7 +326,7 @@ export default class Bucket {
fields?: string[];
} = {}
): Promise<PaginationResult<KintoObject>> {
const path = endpoint.collection(this.name);
const path = this._endpoints.collection(this.name);
return this.client.paginatedList<KintoObject>(path, options, {
headers: this._getHeaders(options),
retry: this._getRetry(options),
Expand Down Expand Up @@ -354,7 +358,7 @@ export default class Bucket {
): Promise<KintoResponse<{}>> {
const { permissions, data = {} } = options;
data.id = id;
const path = endpoint.collection(this.name, id);
const path = this._endpoints.collection(this.name, id);
const request = requests.createRequest(
path,
{ data, permissions },
Expand Down Expand Up @@ -397,7 +401,7 @@ export default class Bucket {
}
const { id } = collectionObj;
const { last_modified } = { ...collectionObj, ...options };
const path = endpoint.collection(this.name, id);
const path = this._endpoints.collection(this.name, id);
const request = requests.deleteRequest(path, {
last_modified,
headers: this._getHeaders(options),
Expand Down Expand Up @@ -430,7 +434,7 @@ export default class Bucket {
fields?: string[];
} = {}
): Promise<PaginationResult<Group>> {
const path = endpoint.group(this.name);
const path = this._endpoints.group(this.name);
return this.client.paginatedList<Group>(path, options, {
headers: this._getHeaders(options),
retry: this._getRetry(options),
Expand Down Expand Up @@ -461,15 +465,16 @@ export default class Bucket {
fields?: string[];
} = {}
): Promise<KintoResponse<Group>> {
let path = endpoint.group(this.name, id);
path = addEndpointOptions(path, options);
const path = this._endpoints.group(this.name, id);
const request = {
headers: this._getHeaders(options),
path,
};
return (
this.client.execute<KintoResponse<Group>>(request, {
retry: this._getRetry(options),
query: options.query,
fields: options.fields,
}) as Promise<KintoResponse<Group>>
);
}
Expand Down Expand Up @@ -504,7 +509,7 @@ export default class Bucket {
id,
members,
};
const path = endpoint.group(this.name, id);
const path = this._endpoints.group(this.name, id);
const { permissions } = options;
const request = requests.createRequest(
path,
Expand Down Expand Up @@ -557,7 +562,7 @@ export default class Bucket {
...options.data,
...group,
};
const path = endpoint.group(this.name, group.id);
const path = this._endpoints.group(this.name, group.id);
const { patch, permissions } = options;
const { last_modified } = { ...data, ...options };
const request = requests.updateRequest(
Expand Down Expand Up @@ -601,7 +606,7 @@ export default class Bucket {
const groupObj = toDataBody(group);
const { id } = groupObj;
const { last_modified } = { ...groupObj, ...options };
const path = endpoint.group(this.name, id);
const path = this._endpoints.group(this.name, id);
const request = requests.deleteRequest(path, {
last_modified,
headers: this._getHeaders(options),
Expand Down Expand Up @@ -631,7 +636,7 @@ export default class Bucket {
): Promise<{ [key in Permission]?: string[] }> {
const request = {
headers: this._getHeaders(options),
path: endpoint.bucket(this.name),
path: this._endpoints.bucket(this.name),
};
const { permissions } =
(await this.client.execute<KintoResponse>(request, {
Expand Down Expand Up @@ -664,7 +669,7 @@ export default class Bucket {
if (!isObject(permissions)) {
throw new Error("A permissions object is required.");
}
const path = endpoint.bucket(this.name);
const path = this._endpoints.bucket(this.name);
const { last_modified } = options;
const data = { last_modified };
const request = requests.updateRequest(
Expand Down Expand Up @@ -706,7 +711,7 @@ export default class Bucket {
if (!isObject(permissions)) {
throw new Error("A permissions object is required.");
}
const path = endpoint.bucket(this.name);
const path = this._endpoints.bucket(this.name);
const { last_modified } = options;
const request = requests.jsonPatchPermissionsRequest(
path,
Expand Down Expand Up @@ -749,7 +754,7 @@ export default class Bucket {
if (!isObject(permissions)) {
throw new Error("A permissions object is required.");
}
const path = endpoint.bucket(this.name);
const path = this._endpoints.bucket(this.name);
const { last_modified } = options;
const request = requests.jsonPatchPermissionsRequest(
path,
Expand Down
Loading