Skip to content

Commit

Permalink
feat: requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xnerhu committed Dec 14, 2021
1 parent db975ee commit 177e9dc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/node": "^14.14.10",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"axios": "^0.24.0",
"c8": "^7.10.0",
"concurrently": "^6.2.1",
"cross-env": "^7.0.3",
Expand All @@ -49,5 +50,9 @@
"rimraf": "^3.0.2",
"ts-jest": "^27.0.7",
"typescript": "^4.1.2"
},
"dependencies": {},
"peerDependencies": {
"axios": "^0.24.0"
}
}
29 changes: 27 additions & 2 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import axios, { AxiosResponse } from "axios";

import { buildWhereQuery } from "./query/where-builder";

export type WhereQuery<T extends Record<string, any>> = {
Expand Down Expand Up @@ -63,12 +65,23 @@ export interface UrlParams {
value?: string;
}

export interface ConnectionOptions {
url: string;
/**
* Headers for every request to Postgrest.
*/
headers?: Record<string, any>;
}

export class QueryBuilder<T extends Record<string, any>> {
private selectOptions?: SelectOptions<T>;

private whereOptions?: WhereQuery<T>;

constructor(private readonly tableName: string) {}
constructor(
private readonly tableName: string,
private readonly connectionOptions: ConnectionOptions,
) {}

public select(opts: SelectOptions<T>) {
this.selectOptions = opts;
Expand Down Expand Up @@ -135,10 +148,22 @@ export class QueryBuilder<T extends Record<string, any>> {

return url;
}

public async get<K = T[]>() {
return await axios
.get<any, AxiosResponse<K>>(
`${this.connectionOptions.url}/${this.toString()}`,
{
headers: this.connectionOptions.headers,
},
)
.then((r) => r.data);
}
}

export const createQuery = <T extends Record<string, any>>(
tableName: string,
connectionOptions: ConnectionOptions,
) => {
return new QueryBuilder<T>(tableName);
return new QueryBuilder<T>(tableName, connectionOptions);
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,13 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=

axios@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.24.0.tgz#804e6fa1e4b9c5288501dd9dff56a7a0940d20d6"
integrity sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==
dependencies:
follow-redirects "^1.14.4"

babel-jest@^27.4.2:
version "27.4.2"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-27.4.2.tgz#6edf80971045cfd44f3f10b6eda6007d95f62742"
Expand Down Expand Up @@ -1718,6 +1725,11 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.2.tgz#64bfed5cb68fe3ca78b3eb214ad97b63bedce561"
integrity sha512-JaTY/wtrcSyvXJl4IMFHPKyFur1sE9AUqc0QnhOaJ0CxHtAoIV8pYDzeEfAaNEtGkOfq4gr3LBFmdXW5mOQFnA==

follow-redirects@^1.14.4:
version "1.14.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.6.tgz#8cfb281bbc035b3c067d6cd975b0f6ade6e855cd"
integrity sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A==

foreground-child@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-2.0.0.tgz#71b32800c9f15aa8f2f83f4a6bd9bff35d861a53"
Expand Down

0 comments on commit 177e9dc

Please sign in to comment.