Skip to content

Commit 1e76daa

Browse files
author
Samuel Bodin
authored
feat: javascript sources client (#97)
1 parent 6dfaf44 commit 1e76daa

File tree

28 files changed

+480
-2
lines changed

28 files changed

+480
-2
lines changed

.github/actions/cache/action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ runs:
8181
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/client-insights/dist
8282
key: ${{ runner.os }}-1-js-client-insights-${{ hashFiles('clients/algoliasearch-client-javascript/client-insights/**') }}-${{ hashFiles('specs/dist/insights.yml') }}
8383

84+
- name: Restore built JavaScript sources client
85+
if: ${{ inputs.job == 'cts' }}
86+
uses: actions/cache@v2
87+
with:
88+
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/client-sources/dist
89+
key: ${{ runner.os }}-1-js-client-sources-${{ hashFiles('clients/algoliasearch-client-javascript/client-sources/**') }}-${{ hashFiles('specs/dist/sources.yml') }}
90+
8491
- name: Restore built Java client
8592
if: ${{ inputs.job == 'cts' }}
8693
uses: actions/cache@v2

.redocly.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ apiDefinitions:
66
query-suggestions: specs/query-suggestions/spec.yml
77
recommend: specs/recommend/spec.yml
88
search: specs/search/spec.yml
9+
sources: specs/sources/spec.yml
910

1011
lint:
1112
extends:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ yarn docker build:clients java recommend
9696

9797
## Testing clients
9898

99-
The clients can be tested inside the [`playground`](./playground) folder
99+
The clients can be tested inside the [`playground`](./playground) folder and with the [common test suite (CTS)](./doc/CTS.md)
100100

101101
### Usage
102102

clients/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This folder hosts the generated clients.
1717
- [@algolia/client-query-suggestions](./algoliasearch-client-javascript/client-query-suggestions/): The Algolia query suggestions client.
1818
- [@algolia/client-search](./algoliasearch-client-javascript/client-search/): The Algolia search client.
1919
- [@algolia/recommend](./algoliasearch-client-javascript/recommend/): The Algolia recommend client.
20+
- [@algolia/sources](./algoliasearch-client-javascript/client-sources/): The Algolia sources client.
2021

2122
#### Utils
2223

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist
3+
.openapi-generator
4+
.env
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
git_push.sh
8+
model/models.ts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This is the entrypoint for the package
2+
export * from './src/apis';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Error.
3+
*/
4+
export type ErrorBase = {
5+
message?: string;
6+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Task } from './task';
2+
3+
export type PostIngestUrlResponse = {
4+
task: Task;
5+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { PostURLJobInput } from './postURLJobInput';
2+
3+
/**
4+
* Object containing a URL job.
5+
*/
6+
export type PostURLJob = {
7+
/**
8+
* The type of the file to ingest.
9+
*/
10+
type: PostURLJobType;
11+
input: PostURLJobInput;
12+
};
13+
14+
export type PostURLJobType = 'csv';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* The input of the job.
3+
*/
4+
export type PostURLJobInput = {
5+
/**
6+
* The URL of the file to ingest.
7+
*/
8+
url: string;
9+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* A task object.
3+
*/
4+
export type Task = {
5+
/**
6+
* The id of the task.
7+
*/
8+
id: string;
9+
/**
10+
* The type of the task executed.
11+
*/
12+
type: TaskType;
13+
};
14+
15+
export type TaskType = 'csv';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@algolia/client-sources",
3+
"version": "0.0.1",
4+
"description": "JavaScript client for @algolia/client-sources",
5+
"repository": "algolia/algoliasearch-client-javascript",
6+
"author": "Algolia",
7+
"private": true,
8+
"license": "MIT",
9+
"main": "dist/api.js",
10+
"types": "dist/api.d.ts",
11+
"scripts": {
12+
"build": "tsc",
13+
"clean": "rm -rf dist/"
14+
},
15+
"engines": {
16+
"node": "^16.0.0",
17+
"yarn": "^3.0.0"
18+
},
19+
"dependencies": {
20+
"@algolia/client-common": "5.0.0"
21+
},
22+
"devDependencies": {
23+
"@types/node": "16.11.11",
24+
"typescript": "4.5.4"
25+
}
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { SourcesApi } from './sourcesApi';
2+
3+
export * from './sourcesApi';
4+
export * from '@algolia/client-common';
5+
6+
export const APIS = [SourcesApi];
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { Transporter } from '@algolia/client-common';
2+
import type {
3+
Headers,
4+
Requester,
5+
Host,
6+
Request,
7+
RequestOptions,
8+
} from '@algolia/client-common';
9+
10+
import type { PostIngestUrlResponse } from '../model/postIngestUrlResponse';
11+
import type { PostURLJob } from '../model/postURLJob';
12+
13+
export class SourcesApi {
14+
protected authentications = {
15+
apiKey: 'Algolia-API-Key',
16+
appId: 'Algolia-Application-Id',
17+
};
18+
19+
private transporter: Transporter;
20+
21+
private applyAuthenticationHeaders(
22+
requestOptions: RequestOptions
23+
): RequestOptions {
24+
if (requestOptions?.headers) {
25+
return {
26+
...requestOptions,
27+
headers: {
28+
...requestOptions.headers,
29+
'X-Algolia-API-Key': this.authentications.apiKey,
30+
'X-Algolia-Application-Id': this.authentications.appId,
31+
},
32+
};
33+
}
34+
35+
return requestOptions;
36+
}
37+
38+
private sendRequest<TResponse>(
39+
request: Request,
40+
requestOptions: RequestOptions
41+
): Promise<TResponse> {
42+
return this.transporter.request(
43+
request,
44+
this.applyAuthenticationHeaders(requestOptions)
45+
);
46+
}
47+
48+
constructor(
49+
appId: string,
50+
apiKey: string,
51+
region: 'de' | 'us',
52+
options?: { requester?: Requester; hosts?: Host[] }
53+
) {
54+
this.setAuthentication({ appId, apiKey });
55+
56+
this.transporter = new Transporter({
57+
hosts: options?.hosts ?? this.getDefaultHosts(region),
58+
baseHeaders: {
59+
'content-type': 'application/x-www-form-urlencoded',
60+
},
61+
userAgent: 'Algolia for Javascript',
62+
timeouts: {
63+
connect: 2,
64+
read: 5,
65+
write: 30,
66+
},
67+
requester: options?.requester,
68+
});
69+
}
70+
71+
getDefaultHosts(region: 'de' | 'us' = 'us'): Host[] {
72+
return [
73+
{
74+
url: `data.${region}.algolia.com`,
75+
accept: 'readWrite',
76+
protocol: 'https',
77+
},
78+
];
79+
}
80+
81+
setRequest(requester: Requester): void {
82+
this.transporter.setRequester(requester);
83+
}
84+
85+
setHosts(hosts: Host[]): void {
86+
this.transporter.setHosts(hosts);
87+
}
88+
89+
setAuthentication({ appId, apiKey }): void {
90+
this.authentications = {
91+
apiKey,
92+
appId,
93+
};
94+
}
95+
96+
/**
97+
* Add an ingestion job that will fetch data from an URL.
98+
*
99+
* @summary Create a new ingestion job via URL.
100+
* @param postURLJob - The postURLJob object.
101+
*/
102+
postIngestUrl(postURLJob: PostURLJob): Promise<PostIngestUrlResponse> {
103+
const path = '/1/ingest/url';
104+
const headers: Headers = { Accept: 'application/json' };
105+
const queryParameters: Record<string, string> = {};
106+
107+
if (!postURLJob) {
108+
throw new Error(
109+
'Parameter `postURLJob` is required when calling `postIngestUrl`.'
110+
);
111+
}
112+
113+
if (!postURLJob.type) {
114+
throw new Error(
115+
'Parameter `postURLJob.type` is required when calling `postIngestUrl`.'
116+
);
117+
}
118+
if (!postURLJob.input) {
119+
throw new Error(
120+
'Parameter `postURLJob.input` is required when calling `postIngestUrl`.'
121+
);
122+
}
123+
124+
const request: Request = {
125+
method: 'POST',
126+
path,
127+
data: postURLJob,
128+
};
129+
130+
const requestOptions: RequestOptions = {
131+
headers,
132+
queryParameters,
133+
};
134+
135+
return this.sendRequest(request, requestOptions);
136+
}
137+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"noImplicitAny": false,
5+
"suppressImplicitAnyIndexErrors": true,
6+
"target": "ES6",
7+
"allowSyntheticDefaultImports": true,
8+
"esModuleInterop": true,
9+
"strict": true,
10+
"moduleResolution": "node",
11+
"removeComments": true,
12+
"sourceMap": true,
13+
"noLib": false,
14+
"declaration": true,
15+
"lib": ["dom", "es6", "es5", "dom.iterable", "scripthost"],
16+
"outDir": "dist",
17+
"typeRoots": ["node_modules/@types"],
18+
"types": ["node"]
19+
},
20+
"include": ["src", "model", "api.ts"],
21+
"exclude": ["dist", "node_modules"]
22+
}

doc/CTS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ It is automaticaly generated for all languages, from a JSON entry point.
55

66
## How to run it
77

8+
> CTS requires all clients to be built
9+
810
```bash
11+
yarn docker build:specs
12+
yarn docker build:clients
913
yarn docker cts:generate
1014
yarn docker cts:test
1115
```

openapitools.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,28 @@
153153
"host": "query-suggestions"
154154
}
155155
},
156+
"javascript-sources": {
157+
"generatorName": "typescript-node",
158+
"templateDir": "#{cwd}/templates/javascript/",
159+
"config": "#{cwd}/openapitools.json",
160+
"apiPackage": "src",
161+
"output": "#{cwd}/clients/algoliasearch-client-javascript/client-sources",
162+
"glob": "specs/dist/sources.yml",
163+
"gitHost": "algolia",
164+
"gitUserId": "algolia",
165+
"gitRepoId": "algoliasearch-client-javascript",
166+
"additionalProperties": {
167+
"modelPropertyNaming": "original",
168+
"supportsES6": true,
169+
"npmName": "@algolia/client-sources",
170+
"npmVersion": "0.0.1",
171+
172+
"packageName": "@algolia/client-sources",
173+
"hasRegionalHost": true,
174+
"isDeHost": true,
175+
"host": "data"
176+
}
177+
},
156178
"java-search": {
157179
"generatorName": "java",
158180
"templateDir": "#{cwd}/templates/java/",

playground/javascript/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
99
"start:query-suggestions": "yarn install && yarn build && yarn test:query-suggestions",
1010
"start:recommend": "yarn install && yarn build && yarn test:recommend",
1111
"start:search": "yarn install && yarn build && yarn test:search",
12+
"start:sources": "yarn install && yarn build && yarn test:sources",
1213
"test:abtesting": "node dist/analytics.js",
1314
"test:analytics": "node dist/analytics.js",
1415
"test:personalization": "node dist/personalization.js",
1516
"test:query-suggestions": "node dist/query-suggestions.js",
1617
"test:recommend": "node dist/recommend.js",
17-
"test:search": "node dist/search.js"
18+
"test:search": "node dist/search.js",
19+
"test:sources": "node dist/sources.js"
1820
},
1921
"devDependencies": {
2022
"@algolia/client-abtesting": "5.0.0",
2123
"@algolia/client-analytics": "5.0.0",
2224
"@algolia/client-personalization": "5.0.0",
2325
"@algolia/client-query-suggestions": "5.0.0",
2426
"@algolia/client-search": "5.0.0",
27+
"@algolia/client-sources": "0.0.1",
2528
"@algolia/recommend": "5.0.0",
2629
"dotenv": "10.0.0",
2730
"typescript": "4.5.4"

0 commit comments

Comments
 (0)