Skip to content

Commit

Permalink
feat: javascript sources client (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Bodin authored Jan 21, 2022
1 parent 6dfaf44 commit 1e76daa
Show file tree
Hide file tree
Showing 28 changed files with 480 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ runs:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/client-insights/dist
key: ${{ runner.os }}-1-js-client-insights-${{ hashFiles('clients/algoliasearch-client-javascript/client-insights/**') }}-${{ hashFiles('specs/dist/insights.yml') }}

- name: Restore built JavaScript sources client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
with:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/client-sources/dist
key: ${{ runner.os }}-1-js-client-sources-${{ hashFiles('clients/algoliasearch-client-javascript/client-sources/**') }}-${{ hashFiles('specs/dist/sources.yml') }}

- name: Restore built Java client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
Expand Down
1 change: 1 addition & 0 deletions .redocly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ apiDefinitions:
query-suggestions: specs/query-suggestions/spec.yml
recommend: specs/recommend/spec.yml
search: specs/search/spec.yml
sources: specs/sources/spec.yml

lint:
extends:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ yarn docker build:clients java recommend

## Testing clients

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

### Usage

Expand Down
1 change: 1 addition & 0 deletions clients/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This folder hosts the generated clients.
- [@algolia/client-query-suggestions](./algoliasearch-client-javascript/client-query-suggestions/): The Algolia query suggestions client.
- [@algolia/client-search](./algoliasearch-client-javascript/client-search/): The Algolia search client.
- [@algolia/recommend](./algoliasearch-client-javascript/recommend/): The Algolia recommend client.
- [@algolia/sources](./algoliasearch-client-javascript/client-sources/): The Algolia sources client.

#### Utils

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
.openapi-generator
.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

git_push.sh
model/models.ts
2 changes: 2 additions & 0 deletions clients/algoliasearch-client-javascript/client-sources/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This is the entrypoint for the package
export * from './src/apis';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Error.
*/
export type ErrorBase = {
message?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Task } from './task';

export type PostIngestUrlResponse = {
task: Task;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { PostURLJobInput } from './postURLJobInput';

/**
* Object containing a URL job.
*/
export type PostURLJob = {
/**
* The type of the file to ingest.
*/
type: PostURLJobType;
input: PostURLJobInput;
};

export type PostURLJobType = 'csv';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The input of the job.
*/
export type PostURLJobInput = {
/**
* The URL of the file to ingest.
*/
url: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* A task object.
*/
export type Task = {
/**
* The id of the task.
*/
id: string;
/**
* The type of the task executed.
*/
type: TaskType;
};

export type TaskType = 'csv';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@algolia/client-sources",
"version": "0.0.1",
"description": "JavaScript client for @algolia/client-sources",
"repository": "algolia/algoliasearch-client-javascript",
"author": "Algolia",
"private": true,
"license": "MIT",
"main": "dist/api.js",
"types": "dist/api.d.ts",
"scripts": {
"build": "tsc",
"clean": "rm -rf dist/"
},
"engines": {
"node": "^16.0.0",
"yarn": "^3.0.0"
},
"dependencies": {
"@algolia/client-common": "5.0.0"
},
"devDependencies": {
"@types/node": "16.11.11",
"typescript": "4.5.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SourcesApi } from './sourcesApi';

export * from './sourcesApi';
export * from '@algolia/client-common';

export const APIS = [SourcesApi];
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { Transporter } from '@algolia/client-common';
import type {
Headers,
Requester,
Host,
Request,
RequestOptions,
} from '@algolia/client-common';

import type { PostIngestUrlResponse } from '../model/postIngestUrlResponse';
import type { PostURLJob } from '../model/postURLJob';

export class SourcesApi {
protected authentications = {
apiKey: 'Algolia-API-Key',
appId: 'Algolia-Application-Id',
};

private transporter: Transporter;

private applyAuthenticationHeaders(
requestOptions: RequestOptions
): RequestOptions {
if (requestOptions?.headers) {
return {
...requestOptions,
headers: {
...requestOptions.headers,
'X-Algolia-API-Key': this.authentications.apiKey,
'X-Algolia-Application-Id': this.authentications.appId,
},
};
}

return requestOptions;
}

private sendRequest<TResponse>(
request: Request,
requestOptions: RequestOptions
): Promise<TResponse> {
return this.transporter.request(
request,
this.applyAuthenticationHeaders(requestOptions)
);
}

constructor(
appId: string,
apiKey: string,
region: 'de' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
hosts: options?.hosts ?? this.getDefaultHosts(region),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
},
userAgent: 'Algolia for Javascript',
timeouts: {
connect: 2,
read: 5,
write: 30,
},
requester: options?.requester,
});
}

getDefaultHosts(region: 'de' | 'us' = 'us'): Host[] {
return [
{
url: `data.${region}.algolia.com`,
accept: 'readWrite',
protocol: 'https',
},
];
}

setRequest(requester: Requester): void {
this.transporter.setRequester(requester);
}

setHosts(hosts: Host[]): void {
this.transporter.setHosts(hosts);
}

setAuthentication({ appId, apiKey }): void {
this.authentications = {
apiKey,
appId,
};
}

/**
* Add an ingestion job that will fetch data from an URL.
*
* @summary Create a new ingestion job via URL.
* @param postURLJob - The postURLJob object.
*/
postIngestUrl(postURLJob: PostURLJob): Promise<PostIngestUrlResponse> {
const path = '/1/ingest/url';
const headers: Headers = { Accept: 'application/json' };
const queryParameters: Record<string, string> = {};

if (!postURLJob) {
throw new Error(
'Parameter `postURLJob` is required when calling `postIngestUrl`.'
);
}

if (!postURLJob.type) {
throw new Error(
'Parameter `postURLJob.type` is required when calling `postIngestUrl`.'
);
}
if (!postURLJob.input) {
throw new Error(
'Parameter `postURLJob.input` is required when calling `postIngestUrl`.'
);
}

const request: Request = {
method: 'POST',
path,
data: postURLJob,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

return this.sendRequest(request, requestOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"target": "ES6",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"noLib": false,
"declaration": true,
"lib": ["dom", "es6", "es5", "dom.iterable", "scripthost"],
"outDir": "dist",
"typeRoots": ["node_modules/@types"],
"types": ["node"]
},
"include": ["src", "model", "api.ts"],
"exclude": ["dist", "node_modules"]
}
4 changes: 4 additions & 0 deletions doc/CTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ It is automaticaly generated for all languages, from a JSON entry point.

## How to run it

> CTS requires all clients to be built
```bash
yarn docker build:specs
yarn docker build:clients
yarn docker cts:generate
yarn docker cts:test
```
Expand Down
22 changes: 22 additions & 0 deletions openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@
"host": "query-suggestions"
}
},
"javascript-sources": {
"generatorName": "typescript-node",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
"output": "#{cwd}/clients/algoliasearch-client-javascript/client-sources",
"glob": "specs/dist/sources.yml",
"gitHost": "algolia",
"gitUserId": "algolia",
"gitRepoId": "algoliasearch-client-javascript",
"additionalProperties": {
"modelPropertyNaming": "original",
"supportsES6": true,
"npmName": "@algolia/client-sources",
"npmVersion": "0.0.1",

"packageName": "@algolia/client-sources",
"hasRegionalHost": true,
"isDeHost": true,
"host": "data"
}
},
"java-search": {
"generatorName": "java",
"templateDir": "#{cwd}/templates/java/",
Expand Down
5 changes: 4 additions & 1 deletion playground/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
"start:query-suggestions": "yarn install && yarn build && yarn test:query-suggestions",
"start:recommend": "yarn install && yarn build && yarn test:recommend",
"start:search": "yarn install && yarn build && yarn test:search",
"start:sources": "yarn install && yarn build && yarn test:sources",
"test:abtesting": "node dist/analytics.js",
"test:analytics": "node dist/analytics.js",
"test:personalization": "node dist/personalization.js",
"test:query-suggestions": "node dist/query-suggestions.js",
"test:recommend": "node dist/recommend.js",
"test:search": "node dist/search.js"
"test:search": "node dist/search.js",
"test:sources": "node dist/sources.js"
},
"devDependencies": {
"@algolia/client-abtesting": "5.0.0",
"@algolia/client-analytics": "5.0.0",
"@algolia/client-personalization": "5.0.0",
"@algolia/client-query-suggestions": "5.0.0",
"@algolia/client-search": "5.0.0",
"@algolia/client-sources": "0.0.1",
"@algolia/recommend": "5.0.0",
"dotenv": "10.0.0",
"typescript": "4.5.4"
Expand Down
Loading

0 comments on commit 1e76daa

Please sign in to comment.