Skip to content

Commit be994e5

Browse files
authored
Merge pull request #94 from masterT/master
Configurable HttpClient
2 parents 92704d6 + 0bd2a26 commit be994e5

File tree

22 files changed

+332
-160
lines changed

22 files changed

+332
-160
lines changed

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,9 @@ import {Model} from 'coloquent';
130130

131131
class Artist extends Model
132132
{
133-
public getJsonApiBaseUrl(): string
134-
{
135-
return 'http://www.app.com/api/';
136-
}
137-
138-
protected jsonApiType = 'artists';
139-
140-
protected pageSize = 30;
133+
static jsonApiBaseUrl = 'http://www.app.com/api'
134+
static jsonApiType = 'artists'
135+
static pageSize = 30
141136
}
142137
```
143138

@@ -151,15 +146,12 @@ import {Model, ToManyRelation, ToOneRelation} from 'coloquent';
151146

152147
abstract class AppModel extends Model
153148
{
154-
getJsonApiBaseUrl(): string
155-
{
156-
return 'http://www.app.com/api/';
157-
}
149+
static jsonApiBaseUrl = 'http://www.app.com/api'
158150
}
159151

160152
class Artist extends AppModel
161153
{
162-
jsonApiType = 'artists';
154+
static jsonApiType = 'artists'
163155

164156
readOnlyAttributes = [
165157
'age'
@@ -198,7 +190,7 @@ class Artist extends AppModel
198190

199191
class Album extends AppModel
200192
{
201-
jsonApiType = 'albums';
193+
static jsonApiType = 'albums'
202194

203195
artist(): ToOneRelation<Artist, this>
204196
{
@@ -223,7 +215,7 @@ class Album extends AppModel
223215

224216
class Song extends AppModel
225217
{
226-
jsonApiType = 'songs';
218+
static jsonApiType = 'songs'
227219

228220
album(): ToOneRelation<Album, this>
229221
{

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@
5252
"moxios": "^0.4.0",
5353
"rimraf": "^3.0.0",
5454
"ts-node": "^8.4.1",
55-
"typescript": "^3.6.4"
55+
"typescript": "^4.5.4"
5656
}
5757
}

src/Builder.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {RetrievalResponse} from "./response/RetrievalResponse";
1717

1818
export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResponse<M> = PluralResponse<M>> implements QueryMethods<M, GET_RESPONSE>
1919
{
20+
protected readonly baseUrl: string;
21+
2022
protected readonly modelType: any;
2123

2224
private readonly httpClient: HttpClient;
@@ -37,13 +39,13 @@ export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResp
3739
forceSingular: boolean = false
3840
) {
3941
this.modelType = modelType;
40-
let modelInstance: M = (new (<any> modelType)());
42+
this.baseUrl = modelType.effectiveJsonApiBaseUrl;
4143
baseModelJsonApiType = baseModelJsonApiType
4244
? baseModelJsonApiType
43-
: modelInstance.getJsonApiType();
45+
: modelType.effectiveJsonApiType;
4446
this.query = new Query(baseModelJsonApiType, queriedRelationName, baseModelJsonApiId);
4547
this.initPaginationSpec();
46-
this.httpClient = modelType.getHttpClient();
48+
this.httpClient = modelType.effectiveHttpClient;
4749
this.forceSingular = forceSingular;
4850
}
4951

@@ -53,7 +55,7 @@ export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResp
5355
clone.getQuery().getPaginationSpec().setPage(page);
5456
if (this.forceSingular) {
5557
return this.getHttpClient()
56-
.get(clone.getQuery().toString())
58+
.get(this.baseUrl + '/' + clone.getQuery().toString())
5759
.then(
5860
(response: HttpClientResponse) => {
5961
return new SingularResponse(clone.getQuery(), response, this.modelType, response.getData()) as unknown as GET_RESPONSE;
@@ -64,7 +66,7 @@ export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResp
6466
);
6567
} else {
6668
return this.getHttpClient()
67-
.get(clone.getQuery().toString())
69+
.get(this.baseUrl + '/' + clone.getQuery().toString())
6870
.then(
6971
(response: HttpClientResponse) => {
7072
return new PluralResponse(clone.getQuery(), response, this.modelType, response.getData(), page) as unknown as GET_RESPONSE;
@@ -81,7 +83,7 @@ export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResp
8183
const clone = this.clone();
8284
clone.getQuery().getPaginationSpec().setPageLimit(1);
8385
return <Promise<SingularResponse<M>>> this.getHttpClient()
84-
.get(this.query.toString())
86+
.get(this.baseUrl + '/' + clone.getQuery().toString())
8587
.then(
8688
(response: HttpClientResponse) => {
8789
return new SingularResponse(this.query, response, this.modelType, response.getData());
@@ -103,7 +105,7 @@ export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResp
103105
const clone = this.clone();
104106
clone.query.setIdToFind(id);
105107
return <Promise<SingularResponse<M>>> clone.getHttpClient()
106-
.get(clone.getQuery().toString())
108+
.get(this.baseUrl + '/' + clone.getQuery().toString())
107109
.then(
108110
(response: HttpClientResponse) => {
109111
return new SingularResponse(clone.getQuery(), response, this.modelType, response.getData());
@@ -163,7 +165,7 @@ export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResp
163165
direction === SortDirection.ASC
164166
)
165167
);
166-
168+
167169
return clone;
168170
}
169171

@@ -218,15 +220,15 @@ export class Builder<M extends Model = Model, GET_RESPONSE extends RetrievalResp
218220
new OffsetBasedPaginationSpec(
219221
this.modelType.getPaginationOffsetParamName(),
220222
this.modelType.getPaginationLimitParamName(),
221-
this.modelType.getPageSize()
223+
this.modelType.pageSize
222224
)
223225
);
224226
} else if (paginationStrategy === PaginationStrategy.PageBased) {
225227
this.query.setPaginationSpec(
226228
new PageBasedPaginationSpec(
227229
this.modelType.getPaginationPageNumberParamName(),
228230
this.modelType.getPaginationPageSizeParamName(),
229-
this.modelType.getPageSize()
231+
this.modelType.pageSize
230232
)
231233
);
232234
} else {

0 commit comments

Comments
 (0)