Skip to content

Commit

Permalink
[FE-2750][FE-2763] Dead simple steel thread and test setup. (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleve-fauna authored Oct 12, 2022
1 parent bfd8694 commit 14201ce
Show file tree
Hide file tree
Showing 8 changed files with 2,451 additions and 23 deletions.
11 changes: 11 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Ticket(s): FE-###

## Problem

## Solution

## Result

## Out of scope

## Testing
5 changes: 0 additions & 5 deletions __tests__/functional/example.test.ts

This file was deleted.

23 changes: 23 additions & 0 deletions __tests__/functional/poc.test.ts
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."));
}
});
});
5 changes: 0 additions & 5 deletions __tests__/integration/example.test.ts

This file was deleted.

1 change: 1 addition & 0 deletions fauna-local-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fql2_enable_endpoint: true
26 changes: 13 additions & 13 deletions package.json
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"
}
}
46 changes: 46 additions & 0 deletions src/index.ts
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.");
}
}
}
Loading

0 comments on commit 14201ce

Please sign in to comment.