Skip to content

Commit

Permalink
Merge pull request #2 from quantcdn/feat/client
Browse files Browse the repository at this point in the history
Feat: Update to support TS natively.
  • Loading branch information
steveworley authored Sep 13, 2023
2 parents 6a8cbcc + f52ecad commit d176d00
Show file tree
Hide file tree
Showing 29 changed files with 8,050 additions and 1,397 deletions.
28 changes: 28 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": "standard-with-typescript",
"overrides": [
{
"env": {
"node": true
},
"files": [
".eslintrc.{js,cjs}"
],
"parserOptions": {
"sourceType": "script",
}
}
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"warnOnUnsupportedTypeScriptVersion": false,
"project": ["./tsconfig.json"]
},
"rules": {
}
}
30 changes: 30 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Compile
on:
workflow_dispatch:

env:
NODE_ENV: production

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
registry-url: https://registry.npmjs.org

- name: Install dependencies
run: npm ci

- name: Build dist
run: npm run build

- name: Publish
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
43 changes: 43 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Test and lint
on: push
env:
NODE_ENV: development
jobs:
test-lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint:src

- name: Test
run: npm run test

- name: Build
run: npm run build

compile:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Trigger the build
uses: actions/github-script@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: workflows } = await github.actions.listRepoWorkflows();
const workflowIdToTrigger = workflows.find(workflow => workflow.name === 'Compile').id;
await github.actions.createWorkflowDispatch({
workflow_id: workflowIdToTrigger,
});
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bower_components
Thumbs.db

# Ignore built ts files
dist/**/*
dist

# ignore yarn.lock
yarn.lock
Expand Down
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
README.md
jest.config.js
tests/
80 changes: 69 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,77 @@ npm i @quantcdn/quant-client

## Usage

The API client expects the following environment variables are present in order to make a successful API connection:
* `QUANT_CUSTOMER`: The organization/customer identifier
* `QUANT_PROJECT`: The project identifier
* `QUANT_TOKEN`: The project token

These values can be found from the "Integrations" page of the QuantCDN Dashboard.
- Import `types` form '@quantcdn/quant-client' to correctly type parameters
- All responses are generated using the `PaginatedResponse` object which provides an async iterator

```
import { QuantClient } from "@quantcdn/quant-client"
import { QuantClient, types } from '@quantcdn/quant-client'
const config:types.Config = {
organization: process.env.QUANT_CUSTOMER,
project: process.env.QUANT_PROJECT,
token: process.env.QUANT_TOKEN
}
// If making organization API requests with the client you need to provide bearer
# config.bearer = process.env.QUANT_BEARER_TOKEN
// Create a new client instance.
const client = new QuantClient(config)
// Purge QuantCDN edge caches for a single route.
QuantClient().purge('/path/to/content')
// Perform requests to the project API.
const p:types.URLPayload = {"url": "/*"}
client.project.purge(p).then(async res => {
const r = await res.first()
console.log(r)
})
// Purge all QuantCDN edge caches.
QuantClient().purge('/*')
// Ping the API to verify details.
client.project.ping().then(async res => {
const b = await res.first()
console.log(b)
})
// Access project metadata.
client.project.meta().then(async res => {
// res.first() will return the first page of the paginated response.
for await (const i of res) {
console.log(i)
}
})
```

## Available methods

The `QuantClient` class has three properties to access different APIs that are exposed by Quant. These are `project`, `organization`, `search`. Both the project and search clients are project specific.

### `client.project`

| Method | Description | Parameters |
| --------- | -------------------------------------- | --------------------- |
| ping | Ping the API to validate credentials | |
| meta | Access the global meta for the project | |
| markup | Send a HTML file | types.MarkupPayload |
| file | Send a non-HTML file | types.FilePayload |
| publish | Publish a URL | types.PublishPayload |
| unpublish | Unpublish a URL | types.PublishPayload |
| redirect | Create a redirect | types.RedirectPayload |
| proxy | Create an origin proxy | types.ProxyPayload |
| delete | Delete a resource | types.URLPayload |
| revisions | Show revisions for a URL | types.URLPayload |
| purge | Purge cache for the given URL | types.URLPayload |

### `client.organization`

| Method | Description | Parameters |
| ------- | ------------------------------------ | -------------------- |
| wafLogs | Access WAF logs for the organization | types.WafLogsPayload |

### `client.search`

| Method | Description | Parameters |
| ------ | --------------------------- | ------------------------ |
| index | Add a new item to the index | types.SearchIndexPayload |
| remove | Remove a URL form the index | types.URLPayload |
| clear | Clear the projects index | |
| status | Get the index status | |
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};
Loading

0 comments on commit d176d00

Please sign in to comment.