Skip to content

Commit 508aeda

Browse files
committed
Merge branch 'feature/1-retrieve-jsonapi-body-meta'
Close #1
2 parents ed37d49 + 1279f68 commit 508aeda

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/lib/query.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { JsonApiBody, JsonApiResource } from "../types/jsonapi.type";
22
import { fromJsonApi, Model, ModelConstructor, ModelInstance } from "./model";
33
import Schema from "./schema";
44

5+
type RawResultType<T> = {
6+
result: T;
7+
body: JsonApiBody<T extends ModelInstance<unknown>[] ? JsonApiResource[] : JsonApiResource>;
8+
}
9+
510
type ExtractDocType<T> =
611
T extends Model<infer U> ? U :
712
T extends Model<infer U>[] ? U :
@@ -37,6 +42,7 @@ interface QueryOptions<DocType> {
3742
sort?: SortQuery<DocType>;
3843
limit?: number;
3944
offset?: number;
45+
raw?: boolean;
4046
}
4147

4248

@@ -99,6 +105,10 @@ class Query<ResultType, DocType> {
99105

100106
options!: QueryOptions<DocType>;
101107

108+
raw!: () => this extends Query<infer ResultType, infer ResultDocType>
109+
? ResultType extends RawResultType<any> ? this : Query<RawResultType<ResultType>, ResultDocType>
110+
: never;
111+
102112
schema!: Schema<DocType>;
103113

104114
setOptions!: (options: QueryOptions<DocType>, overwrite?: boolean) => this;
@@ -172,8 +182,14 @@ Query.prototype.exec = async function exec() {
172182
}
173183
);
174184

175-
return this.model.fromJsonApi(response.data);
185+
if (options.raw) {
186+
return {
187+
result: this.model.fromJsonApi(response.data),
188+
body: response.data,
189+
} as RawResultType<ModelInstance<unknown>[]>;
190+
}
176191

192+
return this.model.fromJsonApi(response.data);
177193
} else if (options.op === 'findById') {
178194
const response = await client.client.get<JsonApiBody<JsonApiResource | null>>(
179195
`/${this.model.type}/${options.id}`,
@@ -182,6 +198,13 @@ Query.prototype.exec = async function exec() {
182198
}
183199
);
184200

201+
if (options.raw) {
202+
return {
203+
result: this.model.fromJsonApi(response.data),
204+
body: response.data,
205+
} as RawResultType<ModelInstance<unknown>>;
206+
}
207+
185208
return this.model.fromJsonApi(response.data);
186209
} else if (options.op === 'findRelationship') {
187210
const response = await client.client.get<JsonApiBody<JsonApiResource | null>>(
@@ -191,7 +214,14 @@ Query.prototype.exec = async function exec() {
191214
}
192215
);
193216

194-
return fromJsonApi(response.data)
217+
if (options.raw) {
218+
return {
219+
result: fromJsonApi(response.data),
220+
body: response.data,
221+
} as RawResultType<ModelInstance<unknown> | ModelInstance<unknown>[]>;
222+
}
223+
224+
return fromJsonApi(response.data);
195225
}
196226

197227
return;
@@ -266,6 +296,13 @@ Query.prototype.offset = function (offset) {
266296
return this;
267297
};
268298

299+
Query.prototype.raw = function () {
300+
this.setOptions({
301+
raw: true,
302+
});
303+
return this;
304+
};
305+
269306
Query.prototype.setOptions = function (options, overwrite) {
270307
if (overwrite) {
271308
this.options = options;

0 commit comments

Comments
 (0)