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

#35 apiPrefix as configuration option #48

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
1 change: 0 additions & 1 deletion .github/FUNDING.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
echo API_TOKEN=${{ secrets.API_TOKEN }} >> .env
- run: npm run build
- run: npm run test:unit
- run: npm run test:e2e
- run: npm run test:integration
- run: npm run lint
env:
CI: true
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"scripts": {
"build": "tsc",
"prepublishOnly": "npm run build && npm run test && npm run lint",
"test": "npm run test:unit && npm run test:e2e",
"prettier": "prettier --write src",
"doc": "typedoc --name \"Confluence.js - Cloud and Server API library\" --out docs ./src/index.ts --plugin typedoc-plugin-extras --footerDate --footerTime --footerTypedocVersion --favicon https://svgshare.com/i/bVi.svg",
"lint": "eslint src tests --ext .ts",
"lint:fix": "npm run lint -- --fix",
"test": "npm run test:unit && npm run test:integration",
"test:unit": "ava tests/unit",
"test:e2e": "ava --timeout=2m --fail-fast --no-worker-threads -c 1 -s tests/e2e/**/*.test.ts"
"test:integration": "ava --timeout=2m --fail-fast --no-worker-threads -c 1 -s tests/integration"
},
"ava": {
"extensions": [
Expand Down
7 changes: 3 additions & 4 deletions src/clients/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Callback } from '../callback';
import type { Client } from './client';
import type { Config } from '../config';
import type { RequestConfig } from '../requestConfig';
import { UtilityTypes } from '../utilityTypes';
import axios, { AxiosInstance } from 'axios';

const ATLASSIAN_TOKEN_CHECK_FLAG = 'X-Atlassian-Token';
Expand All @@ -11,9 +12,7 @@ const ATLASSIAN_TOKEN_CHECK_NOCHECK_VALUE = 'no-check';
export class BaseClient implements Client {
#instance: AxiosInstance | undefined;

protected urlSuffix = '/wiki/rest/';

constructor(protected readonly config: Config) {}
constructor(protected readonly config: UtilityTypes.MarkRequired<Config, 'apiPrefix'>) {}

protected paramSerializer(parameters: Record<string, any>): string {
const parts: string[] = [];
Expand Down Expand Up @@ -72,7 +71,7 @@ export class BaseClient implements Client {
this.#instance = axios.create({
paramsSerializer: this.paramSerializer.bind(this),
...this.config.baseRequestConfig,
baseURL: `${this.config.host}${this.urlSuffix}`,
baseURL: `${this.config.host}${this.config.apiPrefix}`,
headers: this.removeUndefinedProperties({
[ATLASSIAN_TOKEN_CHECK_FLAG]: this.config.noCheckAtlassianToken
? ATLASSIAN_TOKEN_CHECK_NOCHECK_VALUE
Expand Down
8 changes: 8 additions & 0 deletions src/clients/confluenceClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseClient } from './baseClient';
import { Config } from '../config';
import {
Analytics,
Audit,
Expand Down Expand Up @@ -35,6 +36,13 @@ import {
} from '../api';

export class ConfluenceClient extends BaseClient {
constructor(config: Config) {
super({
...config,
apiPrefix: config.apiPrefix ?? '/wiki/rest',
});
}

analytics = new Analytics(this);
audit = new Audit(this);
content = new Content(this);
Expand Down
8 changes: 7 additions & 1 deletion src/clients/serverClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { BaseClient } from './baseClient';
import { Config } from '../config';
import { Audit, Content, ContentBody, Group, LongTask, Search, Space, User } from '../server';

export class ServerClient extends BaseClient {
urlSuffix = '/';
constructor(config: Config) {
super({
...config,
apiPrefix: config.apiPrefix ?? '/',
});
}

audit = new Audit(this);
content = new Content(this);
Expand Down
9 changes: 9 additions & 0 deletions src/clients/version2Client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseClient } from './baseClient';
import { Config } from '../config';
import {
Attachment,
BlogPost,
Expand All @@ -17,6 +18,14 @@ import {
} from '../version2';

export class Version2Client extends BaseClient {
constructor(config: Config) {
super({
...config,
newErrorHandling: true,
apiPrefix: config.apiPrefix ?? '/wiki/api/v2',
});
}

attachment = new Attachment(this);
blogPost = new BlogPost(this);
children = new Children(this);
Expand Down
13 changes: 12 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ export interface Config {
telemetry?: Config.Telemetry;
/** Adds `'X-Atlassian-Token': 'no-check'` to each request header */
noCheckAtlassianToken?: boolean;
/** Enable new API error handling. `false` by default. */
/**
* Enable new API error handling. For "ConfluenceClient" `false` by default.
*
* For "Version2Client" enabled by default.
*/
newErrorHandling?: boolean;
/**
* Prefix for all API routes.
*
* - For "ConfluenceCloud" by default it is "/wiki/rest"
* - For "Version2Client" by default it is "/wiki/api/v2"
*/
apiPrefix?: string;
}

export namespace Config {
Expand Down
2 changes: 2 additions & 0 deletions src/utilityTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export namespace UtilityTypes {

/** Get the XOR type which could make 4 types exclude each other */
export type XOR4<T, U, V, W> = XOR<T, XOR3<U, V, W>>;

export type MarkRequired<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & Required<Pick<T, K>>;
}
File renamed without changes.