-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: javascript sources client (#97)
- Loading branch information
Samuel Bodin
authored
Jan 21, 2022
1 parent
6dfaf44
commit 1e76daa
Showing
28 changed files
with
480 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
clients/algoliasearch-client-javascript/client-sources/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
dist | ||
.openapi-generator | ||
.env |
8 changes: 8 additions & 0 deletions
8
clients/algoliasearch-client-javascript/client-sources/.openapi-generator-ignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
2
clients/algoliasearch-client-javascript/client-sources/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
6 changes: 6 additions & 0 deletions
6
clients/algoliasearch-client-javascript/client-sources/model/errorBase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Error. | ||
*/ | ||
export type ErrorBase = { | ||
message?: string; | ||
}; |
5 changes: 5 additions & 0 deletions
5
clients/algoliasearch-client-javascript/client-sources/model/postIngestUrlResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { Task } from './task'; | ||
|
||
export type PostIngestUrlResponse = { | ||
task: Task; | ||
}; |
14 changes: 14 additions & 0 deletions
14
clients/algoliasearch-client-javascript/client-sources/model/postURLJob.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
9 changes: 9 additions & 0 deletions
9
clients/algoliasearch-client-javascript/client-sources/model/postURLJobInput.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
15 changes: 15 additions & 0 deletions
15
clients/algoliasearch-client-javascript/client-sources/model/task.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
26 changes: 26 additions & 0 deletions
26
clients/algoliasearch-client-javascript/client-sources/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
clients/algoliasearch-client-javascript/client-sources/src/apis.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
137 changes: 137 additions & 0 deletions
137
clients/algoliasearch-client-javascript/client-sources/src/sourcesApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
clients/algoliasearch-client-javascript/client-sources/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.