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

Commit

Permalink
Expose client.endpoints attribute (fixes #641)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Apr 6, 2020
1 parent 19dc21b commit 0f36e49
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
5 changes: 4 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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";
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
39 changes: 21 additions & 18 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 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 @@ -173,7 +176,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 @@ -208,7 +211,7 @@ export default class Bucket {
retry?: number;
} = {}
): Promise<T> {
let path = endpoint.bucket(this.name);
let path = this._endpoints.bucket(this.name);
path = addEndpointOptions(path, options);
const request = {
headers: this._getHeaders(options),
Expand Down Expand Up @@ -256,7 +259,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 @@ -290,7 +293,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 @@ -317,7 +320,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 @@ -349,7 +352,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 @@ -390,7 +393,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 @@ -421,7 +424,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 @@ -452,7 +455,7 @@ export default class Bucket {
fields?: string[];
} = {}
): Promise<KintoResponse<Group>> {
let path = endpoint.group(this.name, id);
let path = this._endpoints.group(this.name, id);
path = addEndpointOptions(path, options);
const request = {
headers: this._getHeaders(options),
Expand Down Expand Up @@ -493,7 +496,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 @@ -544,7 +547,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 @@ -589,7 +592,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 @@ -617,7 +620,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, {
retry: this._getRetry(options),
Expand Down Expand Up @@ -649,7 +652,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 @@ -689,7 +692,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 @@ -730,7 +733,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
34 changes: 18 additions & 16 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { v4 as uuid } from "uuid";

import { capable, toDataBody, isObject } from "./utils";
import * as requests from "./requests";
import endpoint from "./endpoint";
import { addEndpointOptions } from "./utils";
import KintoClientBase, { PaginatedListParams, PaginationResult } from "./base";
import Bucket from "./bucket";
Expand Down Expand Up @@ -34,6 +33,7 @@ export default class Collection {
public client: KintoClientBase;
private bucket: Bucket;
public name: string;
private _endpoints: any;
private _retry: number;
private _safe: boolean;
private _headers: Record<string, string>;
Expand Down Expand Up @@ -71,6 +71,8 @@ export default class Collection {
*/
this.name = name;

this._endpoints = client.endpoints;

/**
* @ignore
*/
Expand Down Expand Up @@ -133,7 +135,7 @@ export default class Collection {
async getTotalRecords(
options: { headers?: Record<string, string>; retry?: number } = {}
): Promise<number> {
const path = endpoint.record(this.bucket.name, this.name);
const path = this._endpoints.record(this.bucket.name, this.name);
const request: KintoRequest = {
headers: this._getHeaders(options),
path,
Expand All @@ -158,7 +160,7 @@ export default class Collection {
async getRecordsTimestamp(
options: { headers?: Record<string, string>; retry?: number } = {}
): Promise<string | null> {
const path = endpoint.record(this.bucket.name, this.name);
const path = this._endpoints.record(this.bucket.name, this.name);
const request: KintoRequest = {
headers: this._getHeaders(options),
path,
Expand Down Expand Up @@ -193,7 +195,7 @@ export default class Collection {
retry?: number;
} = {}
): Promise<T> {
let path = endpoint.collection(this.bucket.name, this.name);
let path = this._endpoints.collection(this.bucket.name, this.name);
path = addEndpointOptions(path, options);
const request = { headers: this._getHeaders(options), path };
const { data } = (await this.client.execute(request, {
Expand Down Expand Up @@ -231,7 +233,7 @@ export default class Collection {
const { patch, permissions } = options;
const { last_modified } = { ...data, ...options };

const path = endpoint.collection(this.bucket.name, this.name);
const path = this._endpoints.collection(this.bucket.name, this.name);
const request = requests.updateRequest(
path,
{ data, permissions },
Expand Down Expand Up @@ -262,7 +264,7 @@ export default class Collection {
retry?: number;
} = {}
): Promise<{ [key in Permission]?: string[] }> {
const path = endpoint.collection(this.bucket.name, this.name);
const path = this._endpoints.collection(this.bucket.name, this.name);
const request = { headers: this._getHeaders(options), path };
const { permissions } = (await this.client.execute<KintoResponse>(request, {
retry: this._getRetry(options),
Expand Down Expand Up @@ -294,7 +296,7 @@ export default class Collection {
if (!isObject(permissions)) {
throw new Error("A permissions object is required.");
}
const path = endpoint.collection(this.bucket.name, this.name);
const path = this._endpoints.collection(this.bucket.name, this.name);
const data = { last_modified: options.last_modified };
const request = requests.updateRequest(
path,
Expand Down Expand Up @@ -333,7 +335,7 @@ export default class Collection {
if (!isObject(permissions)) {
throw new Error("A permissions object is required.");
}
const path = endpoint.collection(this.bucket.name, this.name);
const path = this._endpoints.collection(this.bucket.name, this.name);
const { last_modified } = options;
const request = requests.jsonPatchPermissionsRequest(
path,
Expand Down Expand Up @@ -374,7 +376,7 @@ export default class Collection {
if (!isObject(permissions)) {
throw new Error("A permissions object is required.");
}
const path = endpoint.collection(this.bucket.name, this.name);
const path = this._endpoints.collection(this.bucket.name, this.name);
const { last_modified } = options;
const request = requests.jsonPatchPermissionsRequest(
path,
Expand Down Expand Up @@ -413,7 +415,7 @@ export default class Collection {
} = {}
): Promise<KintoResponse<T>> {
const { permissions } = options;
const path = endpoint.record(this.bucket.name, this.name, record.id);
const path = this._endpoints.record(this.bucket.name, this.name, record.id);
const request = requests.createRequest(
path,
{ data: record, permissions },
Expand Down Expand Up @@ -463,7 +465,7 @@ export default class Collection {
> {
const { permissions } = options;
const id = record.id || uuid();
const path = endpoint.attachment(this.bucket.name, this.name, id);
const path = this._endpoints.attachment(this.bucket.name, this.name, id);
const { last_modified } = { ...record, ...options };
const addAttachmentRequest = requests.addAttachmentRequest(
path,
Expand Down Expand Up @@ -506,7 +508,7 @@ export default class Collection {
} = {}
): Promise<{}> {
const { last_modified } = options;
const path = endpoint.attachment(this.bucket.name, this.name, recordId);
const path = this._endpoints.attachment(this.bucket.name, this.name, recordId);
const request = requests.deleteRequest(path, {
last_modified,
headers: this._getHeaders(options),
Expand Down Expand Up @@ -549,7 +551,7 @@ export default class Collection {
}
const { permissions } = options;
const { last_modified } = { ...record, ...options };
const path = endpoint.record(this.bucket.name, this.name, record.id);
const path = this._endpoints.record(this.bucket.name, this.name, record.id);
const request = requests.updateRequest(
path,
{ data: record, permissions },
Expand Down Expand Up @@ -592,7 +594,7 @@ export default class Collection {
}
const { id } = recordObj;
const { last_modified } = { ...recordObj, ...options };
const path = endpoint.record(this.bucket.name, this.name, id);
const path = this._endpoints.record(this.bucket.name, this.name, id);
const request = requests.deleteRequest(path, {
last_modified,
headers: this._getHeaders(options),
Expand Down Expand Up @@ -627,7 +629,7 @@ export default class Collection {
retry?: number;
} = {}
): Promise<KintoResponse<T>> {
let path = endpoint.record(this.bucket.name, this.name, id);
let path = this._endpoints.record(this.bucket.name, this.name, id);
path = addEndpointOptions(path, options);
const request = { headers: this._getHeaders(options), path };
return this.client.execute<KintoResponse<T>>(request, {
Expand Down Expand Up @@ -678,7 +680,7 @@ export default class Collection {
at?: number;
} = {}
): Promise<PaginationResult<T>> {
const path = endpoint.record(this.bucket.name, this.name);
const path = this._endpoints.record(this.bucket.name, this.name);
if (options.at) {
return this.getSnapshot<T>(options.at);
} else {
Expand Down
File renamed without changes.

0 comments on commit 0f36e49

Please sign in to comment.