-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
bfd8694
commit 14201ce
Showing
8 changed files
with
2,451 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Ticket(s): FE-### | ||
|
||
## Problem | ||
|
||
## Solution | ||
|
||
## Result | ||
|
||
## Out of scope | ||
|
||
## Testing |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Client, endpoints, QueryError } from "../../src"; | ||
|
||
describe("query", () => { | ||
const client = new Client({ | ||
endpoint: endpoints.local, | ||
secret: "secret", | ||
queryTimeoutMillis: 60, | ||
}); | ||
|
||
it("Can query an FQL-x endpoint", async () => { | ||
const result = await client.query<number>('"taco".length'); | ||
expect(result).toEqual(4); | ||
}); | ||
|
||
it("Throws an error if the query is invalid", async () => { | ||
expect.assertions(1); | ||
try { | ||
await client.query<number>('"taco".length;'); | ||
} catch (e) { | ||
expect(e).toEqual(new QueryError("Query failed.")); | ||
} | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fql2_enable_endpoint: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
{ | ||
"name": "your repo name", | ||
"version": "1.0.0", | ||
"description": "your description", | ||
"name": "fauna", | ||
"version": "10.0.0", | ||
"description": "TODO", | ||
"main": "index.js", | ||
"repository": "your repo url", | ||
"repository": "https://github.com/fauna/fauna-js.git", | ||
"author": "Fauna", | ||
"private": true, | ||
"dependencies": { | ||
"axios": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"@tsconfig/node16-strictest": "^1.0.3", | ||
"@types/jest": "^28.1.6", | ||
"@types/node": "^18.6.3", | ||
"husky": "^7.0.0", | ||
"jest": "^28.1.3", | ||
"@tsconfig/node16-strictest": "^1.0.4", | ||
"@types/jest": "^29.1.2", | ||
"@types/node": "^18.8.3", | ||
"husky": "^8.0.1", | ||
"jest": "^29.1.2", | ||
"prettier": "^2.7.1", | ||
"pretty-quick": "^3.1.3", | ||
"ts-jest": "^28.0.7", | ||
"typescript": "^4.7.4" | ||
"ts-jest": "^29.0.3", | ||
"typescript": "^4.8.4" | ||
}, | ||
"scripts": { | ||
"prepare": "husky install", | ||
"build": "tsc --project ./", | ||
"sync-submodules": "git submodule sync; git submodule update --init --recursive --remote", | ||
"fauna-local": "docker start faunadb || docker run --rm -d --name faunadb -p 8443:8443 -p 8084:8084 fauna/faunadb:latest", | ||
"fauna-local": "docker start faunadb-fql-x || docker run --rm -d --name faunadb-fql-x -p 8443:8443 -p 8084:8084 -v $PWD/fauna-local-config.yml:/fauna/etc/fauna-local-config.yml gcr.io/faunadb-cloud/faunadb/enterprise --config /fauna/etc/fauna-local-config.yml", | ||
"test": "jest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import axios, { Axios, AxiosResponse } from "axios"; | ||
|
||
export interface ClientConfiguration { | ||
endpoint: URL; | ||
secret: string; | ||
queryTimeoutMillis: number; | ||
} | ||
|
||
export interface Endpoints { | ||
cloud: URL; | ||
local: URL; | ||
} | ||
|
||
export const endpoints: Endpoints = { | ||
cloud: new URL("https://db.fauna.com"), | ||
local: new URL("http://localhost:8443"), | ||
}; | ||
|
||
export class QueryError extends Error {} | ||
|
||
export class Client { | ||
readonly clientConfiguration: ClientConfiguration; | ||
readonly client: Axios; | ||
|
||
constructor(clientConfiguration: ClientConfiguration) { | ||
this.clientConfiguration = clientConfiguration; | ||
this.client = axios.create({ | ||
baseURL: this.clientConfiguration.endpoint.toString(), | ||
timeout: this.clientConfiguration.queryTimeoutMillis + 1000, | ||
}); | ||
this.client.defaults.headers.common[ | ||
"Authorization" | ||
] = `Bearer ${this.clientConfiguration.secret}`; | ||
} | ||
|
||
async query<T = any>(query: string): Promise<T> { | ||
try { | ||
const result: AxiosResponse<{ data: T }> = await this.client.post<{ | ||
data: T; | ||
}>("/query/1", { query }); | ||
return result.data.data; | ||
} catch (e) { | ||
throw new QueryError("Query failed."); | ||
} | ||
} | ||
} |
Oops, something went wrong.