Skip to content

Commit 2ce87c4

Browse files
committed
Moved element helpers from CollectionEndpoint to GenericCollectionEndpoint
1 parent 37fb7cf commit 2ce87c4

2 files changed

Lines changed: 50 additions & 48 deletions

File tree

endpoints/generic/CollectionEndpoint.ts

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { GenericCollectionEndpoint, ElementEndpoint } from ".";
22
import { Endpoint } from "../Endpoint";
33

44
/**
5-
* Endpoint for a collection of `TEntity`s addressable as `ElementEndpoint<TEntity>`s.
5+
* Endpoint for a collection of `TEntity`s addressable as `ElementEndpoint<TEntity>`s.<br>
6+
* Use {@link GenericCollectionEndpoint} instead if you wish to customize the element endpoint type.
67
* @typeParam TEntity The type of individual elements in the collection.
78
*/
89
export class CollectionEndpoint<TEntity> extends GenericCollectionEndpoint<TEntity, ElementEndpoint<TEntity>> {
@@ -14,47 +15,4 @@ export class CollectionEndpoint<TEntity> extends GenericCollectionEndpoint<TEnti
1415
constructor(referrer: Endpoint, relativeUri: URL | string) {
1516
super(referrer, relativeUri, ElementEndpoint);
1617
}
17-
18-
/**
19-
* Determines whether the collection contains a specific element.
20-
* @param element The ID identifying the entity or an entity to extract the ID from.
21-
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
22-
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
23-
* @throws {@link HttpError}: Other non-success status code
24-
*/
25-
contains(element: (TEntity | string)) { return this.get(element).exists(); }
26-
27-
/**
28-
* Sets/replaces an existing element in the collection.
29-
* @param element The new state of the element.
30-
* @returns The `TEntity` as returned by the server, possibly with additional fields set. undefined if the server does not respond with a result entity.
31-
* @throws {@link BadRequestError}: {@link HttpStatusCode.BadRequest}
32-
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
33-
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
34-
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
35-
* @throws {@link HttpError}: Other non-success status code
36-
*/
37-
set(element: TEntity) { return this.get(element).set(element); }
38-
39-
/**
40-
* Modifies an existing element in the collection by merging changes on the server-side.
41-
* @param element The `TEntity` data to merge with the existing element.
42-
* @returns The `TEntity` as returned by the server, possibly with additional fields set. undefined if the server does not respond with a result entity.
43-
* @throws {@link BadRequestError}: {@link HttpStatusCode.BadRequest}
44-
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
45-
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
46-
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
47-
* @throws {@link HttpError}: Other non-success status code
48-
*/
49-
merge(element: TEntity) { return this.get(element).merge(element); }
50-
51-
/**
52-
* Deletes an existing element from the collection.
53-
* @param element The ID identifying the entity or an entity to extract the ID from.
54-
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
55-
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
56-
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
57-
* @throws {@link HttpError}: Other non-success status code
58-
*/
59-
delete(element: (TEntity | string)) { return this.get(element).delete(); }
6018
}

endpoints/generic/GenericCollectionEndpoint.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { ETagEndpointBase } from ".";
1+
import { ETagEndpointBase, ElementEndpoint } from ".";
22
import { Endpoint } from "../Endpoint";
33
import { CachingEndpoint } from "../CachingEndpoint";
44
import { HttpMethod, HttpHeader, ResponseCache } from "../../http";
55

66
/**
7-
* Endpoint for a collection of `TEntity`s addressable as `TElementEndpoint`s.
7+
* Endpoint for a collection of `TEntity`s addressable as `TElementEndpoint`s.<br>
8+
* Use {@link CollectionEndpoint} instead if you wish to use the default {@link ElementEndpoint} type.
89
* @typeParam TEntity The type of individual elements in the collection.
9-
* @typeParam TElementEndpoint The type of {@link Endpoint}s to provide for individual `TEntity`s.
10+
* @typeParam TElementEndpoint The type of {@link ElementEndpoint} to provide for individual `TEntity`s.
1011
*/
11-
export class GenericCollectionEndpoint<TEntity, TElementEndpoint extends Endpoint> extends ETagEndpointBase {
12+
export class GenericCollectionEndpoint<TEntity, TElementEndpoint extends ElementEndpoint<TEntity>> extends ETagEndpointBase {
1213
/**
1314
* Creates a new collection endpoint.
1415
* @param referrer The endpoint used to navigate to this one.
@@ -114,4 +115,47 @@ export class GenericCollectionEndpoint<TEntity, TElementEndpoint extends Endpoin
114115
* @throws {@link HttpError}: Other non-success status code
115116
*/
116117
async setAll(entities: TEntity[]) { await this.putContent(entities); }
118+
119+
/**
120+
* Determines whether the collection contains a specific element.
121+
* @param element The ID identifying the entity or an entity to extract the ID from.
122+
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
123+
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
124+
* @throws {@link HttpError}: Other non-success status code
125+
*/
126+
contains(element: (TEntity | string)) { return this.get(element).exists(); }
127+
128+
/**
129+
* Sets/replaces an existing element in the collection.
130+
* @param element The new state of the element.
131+
* @returns The `TEntity` as returned by the server, possibly with additional fields set. undefined if the server does not respond with a result entity.
132+
* @throws {@link BadRequestError}: {@link HttpStatusCode.BadRequest}
133+
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
134+
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
135+
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
136+
* @throws {@link HttpError}: Other non-success status code
137+
*/
138+
set(element: TEntity) { return this.get(element).set(element); }
139+
140+
/**
141+
* Modifies an existing element in the collection by merging changes on the server-side.
142+
* @param element The `TEntity` data to merge with the existing element.
143+
* @returns The `TEntity` as returned by the server, possibly with additional fields set. undefined if the server does not respond with a result entity.
144+
* @throws {@link BadRequestError}: {@link HttpStatusCode.BadRequest}
145+
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
146+
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
147+
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
148+
* @throws {@link HttpError}: Other non-success status code
149+
*/
150+
merge(element: TEntity) { return this.get(element).merge(element); }
151+
152+
/**
153+
* Deletes an existing element from the collection.
154+
* @param element The ID identifying the entity or an entity to extract the ID from.
155+
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
156+
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
157+
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
158+
* @throws {@link HttpError}: Other non-success status code
159+
*/
160+
delete(element: (TEntity | string)) { return this.get(element).delete(); }
117161
}

0 commit comments

Comments
 (0)