From 0f36e4953c58a74b4419bc47e267ab535eeb3fbe Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Mon, 6 Apr 2020 16:56:10 +0200 Subject: [PATCH] Expose client.endpoints attribute (fixes #641) --- src/base.ts | 5 +++- src/bucket.ts | 39 +++++++++++++++++-------------- src/collection.ts | 34 ++++++++++++++------------- src/{endpoint.ts => endpoints.ts} | 0 4 files changed, 43 insertions(+), 35 deletions(-) rename src/{endpoint.ts => endpoints.ts} (100%) diff --git a/src/base.ts b/src/base.ts index 34fcc00d..e51b1fd9 100644 --- a/src/base.ts +++ b/src/base.ts @@ -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"; @@ -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; @@ -141,6 +142,8 @@ export default class KintoClientBase { */ this.events = options.events; + this.endpoints = endpoints; + const { requestMode, timeout } = options; /** * The HTTP instance. diff --git a/src/bucket.ts b/src/bucket.ts index 538f5619..2e0fea54 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -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, @@ -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; @@ -57,6 +57,9 @@ export default class Bucket { * @type {String} */ this.name = name; + + this._endpoints = client.endpoints; + /** * @ignore */ @@ -145,7 +148,7 @@ export default class Bucket { retry?: number; } = {} ): Promise { - const path = endpoint.collection(this.name); + const path = this._endpoints.collection(this.name); const request: KintoRequest = { headers: this._getHeaders(options), path, @@ -173,7 +176,7 @@ export default class Bucket { retry?: number; } = {} ): Promise { - const path = endpoint.group(this.name); + const path = this._endpoints.group(this.name); const request: KintoRequest = { headers: this._getHeaders(options), path, @@ -208,7 +211,7 @@ export default class Bucket { retry?: number; } = {} ): Promise { - let path = endpoint.bucket(this.name); + let path = this._endpoints.bucket(this.name); path = addEndpointOptions(path, options); const request = { headers: this._getHeaders(options), @@ -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( @@ -290,7 +293,7 @@ export default class Bucket { retry?: number; } = {} ): Promise>> { - const path = endpoint.history(this.name); + const path = this._endpoints.history(this.name); return this.client.paginatedList>(path, options, { headers: this._getHeaders(options), retry: this._getRetry(options), @@ -317,7 +320,7 @@ export default class Bucket { fields?: string[]; } = {} ): Promise> { - const path = endpoint.collection(this.name); + const path = this._endpoints.collection(this.name); return this.client.paginatedList(path, options, { headers: this._getHeaders(options), retry: this._getRetry(options), @@ -349,7 +352,7 @@ export default class Bucket { ): Promise> { 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 }, @@ -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), @@ -421,7 +424,7 @@ export default class Bucket { fields?: string[]; } = {} ): Promise> { - const path = endpoint.group(this.name); + const path = this._endpoints.group(this.name); return this.client.paginatedList(path, options, { headers: this._getHeaders(options), retry: this._getRetry(options), @@ -452,7 +455,7 @@ export default class Bucket { fields?: string[]; } = {} ): Promise> { - 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), @@ -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, @@ -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( @@ -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), @@ -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(request, { retry: this._getRetry(options), @@ -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( @@ -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, @@ -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, diff --git a/src/collection.ts b/src/collection.ts index cc1f90c5..8a44c357 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -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"; @@ -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; @@ -71,6 +71,8 @@ export default class Collection { */ this.name = name; + this._endpoints = client.endpoints; + /** * @ignore */ @@ -133,7 +135,7 @@ export default class Collection { async getTotalRecords( options: { headers?: Record; retry?: number } = {} ): Promise { - 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, @@ -158,7 +160,7 @@ export default class Collection { async getRecordsTimestamp( options: { headers?: Record; retry?: number } = {} ): Promise { - 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, @@ -193,7 +195,7 @@ export default class Collection { retry?: number; } = {} ): Promise { - 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, { @@ -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 }, @@ -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(request, { retry: this._getRetry(options), @@ -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, @@ -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, @@ -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, @@ -413,7 +415,7 @@ export default class Collection { } = {} ): Promise> { 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 }, @@ -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, @@ -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), @@ -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 }, @@ -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), @@ -627,7 +629,7 @@ export default class Collection { retry?: number; } = {} ): Promise> { - 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>(request, { @@ -678,7 +680,7 @@ export default class Collection { at?: number; } = {} ): Promise> { - 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(options.at); } else { diff --git a/src/endpoint.ts b/src/endpoints.ts similarity index 100% rename from src/endpoint.ts rename to src/endpoints.ts