Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the same eslint config as API #5

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
**/*.js
27 changes: 27 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
}
}
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
node-version: "20"
cache: "npm"

- name: Install dependencies
run: npm install
Expand All @@ -38,11 +38,11 @@ jobs:
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
node-version: "20"
cache: "npm"

- name: Install dependencies
run: npm install

- name: Build
run: npm run build
run: npm run build
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Empty file added .prettierrc
Empty file.
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["esbenp.prettier-vscode"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will prompt devs to install the plugin.

}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ This repo uses `vitest` to run tests. There is a pulumi environment called `npm-

```console
$ pulumi env run npm-test npm run test
```
```
84 changes: 43 additions & 41 deletions api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,47 @@ type Method = "POST" | "GET" | "PUT" | "DELETE";
// body can be any object
/* eslint-disable @typescript-eslint/no-explicit-any */


export class CortexApiClient {
constructor(private org: string, private apiUrl: string, private accessToken: string) { }

async POST(path: string, body?: any) {
return this.makeRequest("POST", path, body);
}

async PUT(path: string, body?: any) {
return this.makeRequest("PUT", path, body);
}

async GET(path: string, body?: any) {
return this.makeRequest("GET", path, body);
}

async DELETE(path: string, body?: any) {
return this.makeRequest("DELETE", path, body);
}

async POSTForm(path: string, form: FormData) {
return fetch(`${this.apiUrl}/org/${this.org}${path}`, {
method: "POST",
headers: {
"Authorization": `Bearer ${this.accessToken}`
},
body: form,
})
}

private async makeRequest(method: Method, path: string, body?: any) {
return fetch(`${this.apiUrl}/org/${this.org}${path}`, {
method,
headers: {
"Authorization": `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : undefined,
})
}

}
constructor(
private org: string,
private apiUrl: string,
private accessToken: string,
) {}

async POST(path: string, body?: any) {
return this.makeRequest("POST", path, body);
}

async PUT(path: string, body?: any) {
return this.makeRequest("PUT", path, body);
}

async GET(path: string, body?: any) {
return this.makeRequest("GET", path, body);
}

async DELETE(path: string, body?: any) {
return this.makeRequest("DELETE", path, body);
}

async POSTForm(path: string, form: FormData) {
return fetch(`${this.apiUrl}/org/${this.org}${path}`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.accessToken}`,
},
body: form,
});
}

private async makeRequest(method: Method, path: string, body?: any) {
return fetch(`${this.apiUrl}/org/${this.org}${path}`, {
method,
headers: {
Authorization: `Bearer ${this.accessToken}`,
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : undefined,
});
}
}
28 changes: 15 additions & 13 deletions catalog.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test, afterEach } from 'vitest'
import { expect, test, afterEach } from "vitest";
import { CortexClient } from "./index";
import { Catalog, CatalogConfig } from "./catalog";

Expand All @@ -15,15 +15,13 @@ afterEach(async () => {
if (catalog) {
await catalog.delete();
}
}
catch {
// probably catalog has already been deleted by a test, so ignore
} catch {
// probably catalog has already been deleted by a test, so ignore
}
});

test('Catalog CRUD', async () => {

const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`
test("Catalog CRUD", async () => {
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;

const config: CatalogConfig = {
description: "foo bar",
Expand All @@ -39,8 +37,8 @@ test('Catalog CRUD', async () => {
expect(catalog.config.description).toBe(config.description);

// update
config.description = "buzz 123"
config.instructions = ["1", "2", "3"]
config.description = "buzz 123";
config.instructions = ["1", "2", "3"];
catalog = await client.configureCatalog(catalogName, config);

catalog = await client.getCatalog(catalogName);
Expand All @@ -54,16 +52,20 @@ test('Catalog CRUD', async () => {

// list
const catalogList = await client.listCatalogs();
const catalogFromList = catalogList.find(c => c.name === catalogName);
const catalogFromList = catalogList.find((c) => c.name === catalogName);
expect(catalogFromList).toBeDefined();
expect(catalogFromList?.documentCount).toBe(0);
expect(catalogFromList?.description).toBe(config.description);
// get catalog from list result
const getCatalogFromList = await catalogFromList?.Catalog();
expect(getCatalogFromList?.config.instructions).toStrictEqual(config.instructions);
expect(getCatalogFromList?.config.instructions).toStrictEqual(
config.instructions,
);

// delete
// delete
await catalog.delete();
// assert that the get fails
await expect(async () => { await client.getCatalog(catalogName) }).rejects.toThrowError("Failed to get catalog: Not Found");
await expect(async () => {
await client.getCatalog(catalogName);
}).rejects.toThrowError("Failed to get catalog: Not Found");
});
Loading