Skip to content

Commit 1963b8e

Browse files
authored
fix: clean code generation and types (#12)
1 parent 3ff5dcc commit 1963b8e

38 files changed

+51
-115
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ yarn client:build
2020

2121
# Testing clients
2222

23-
Go to the [`playground`](./playground) folder to test your client
23+
The clients can be tested inside the [`playground`](./playground) folder
2424

2525
## JavaScript
2626

2727
```bash
28-
cd playground/javascript && yarn start
28+
yarn playground:js
2929
```
3030

3131
# Troubleshooting
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
export * from './searchApi';
22
import { SearchApi } from './searchApi';
3-
import * as http from 'http';
4-
5-
export class HttpError extends Error {
6-
constructor(public response: http.IncomingMessage, public body: any, public statusCode?: number) {
7-
super('HTTP request failed');
8-
this.name = 'HttpError';
9-
}
10-
}
11-
12-
export { RequestFile } from '../model/models';
133

144
export class searchClient extends SearchApi {}
155

6+
export * from '../utils/errors';
7+
168
export const APIS = [SearchApi];

clients/algoliasearch-client-javascript/client-search/searchApi.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Headers, Host, Request, RequestOptions } from '../utils/types';
55

66
import { BatchObject } from '../model/batchObject';
77
import { BatchResponse } from '../model/batchResponse';
8+
import { ErrorBase } from '../model/errorBase';
89
import { MultipleQueriesObject } from '../model/multipleQueriesObject';
910
import { MultipleQueriesResponse } from '../model/multipleQueriesResponse';
1011
import { SaveObjectResponse } from '../model/saveObjectResponse';
@@ -15,8 +16,6 @@ import { SearchResponse } from '../model/searchResponse';
1516
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
1617
import { HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
1718

18-
import { HttpError, RequestFile } from './apis';
19-
2019
export enum SearchApiApiKeys {
2120
apiKey,
2221
appId,

clients/algoliasearch-client-javascript/model/batchObject.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestFile } from './models';
21
import { Operation } from './operation';
32

43
export class BatchObject {

clients/algoliasearch-client-javascript/model/batchResponse.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class BatchResponse {
42
/**
53
* taskID of the indexing task to wait for.

clients/algoliasearch-client-javascript/model/modelError.ts renamed to clients/algoliasearch-client-javascript/model/errorBase.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { RequestFile } from './models';
2-
31
/**
42
* Error
53
*/
6-
export class ModelError extends null<String, object> {
4+
// export class ErrorBase extends null<String, object>
5+
export class ErrorBase {
76
'message'?: string;
87

98
static discriminator: string | undefined = undefined;
@@ -17,6 +16,6 @@ export class ModelError extends null<String, object> {
1716
];
1817

1918
static getAttributeTypeMap() {
20-
return super.getAttributeTypeMap().concat(ModelError.attributeTypeMap);
19+
return ErrorBase.attributeTypeMap;
2120
}
2221
}

clients/algoliasearch-client-javascript/model/highlightResult.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class HighlightResult {
42
/**
53
* Markup text with occurrences highlighted.

clients/algoliasearch-client-javascript/model/models.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { RequestOptions } from '../utils/types';
22

33
export * from './batchObject';
44
export * from './batchResponse';
5+
export * from './errorBase';
56
export * from './highlightResult';
6-
export * from './modelError';
77
export * from './multipleQueries';
88
export * from './multipleQueriesObject';
99
export * from './multipleQueriesResponse';
@@ -18,22 +18,10 @@ export * from './searchResponse';
1818
export * from './searchResponseFacetsStats';
1919
export * from './snippetResult';
2020

21-
import * as fs from 'fs';
22-
23-
export interface RequestDetailedFile {
24-
value: Buffer;
25-
options?: {
26-
filename?: string;
27-
contentType?: string;
28-
};
29-
}
30-
31-
export type RequestFile = string | Buffer | fs.ReadStream | RequestDetailedFile;
32-
3321
import { BatchObject } from './batchObject';
3422
import { BatchResponse } from './batchResponse';
23+
import { ErrorBase } from './errorBase';
3524
import { HighlightResult } from './highlightResult';
36-
import { ModelError } from './modelError';
3725
import { MultipleQueries } from './multipleQueries';
3826
import { MultipleQueriesObject } from './multipleQueriesObject';
3927
import { MultipleQueriesResponse } from './multipleQueriesResponse';
@@ -67,8 +55,8 @@ let enumsMap: { [index: string]: any } = {
6755
let typeMap: { [index: string]: any } = {
6856
BatchObject: BatchObject,
6957
BatchResponse: BatchResponse,
58+
ErrorBase: ErrorBase,
7059
HighlightResult: HighlightResult,
71-
ModelError: ModelError,
7260
MultipleQueries: MultipleQueries,
7361
MultipleQueriesObject: MultipleQueriesObject,
7462
MultipleQueriesResponse: MultipleQueriesResponse,

clients/algoliasearch-client-javascript/model/multipleQueries.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class MultipleQueries {
42
/**
53
* The Algolia index name

clients/algoliasearch-client-javascript/model/multipleQueriesObject.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestFile } from './models';
21
import { MultipleQueries } from './multipleQueries';
32

43
export class MultipleQueriesObject {

clients/algoliasearch-client-javascript/model/multipleQueriesResponse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestFile } from './models';
21
import { SearchResponse } from './searchResponse';
32

43
export class MultipleQueriesResponse {

clients/algoliasearch-client-javascript/model/operation.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class Operation {
42
/**
53
* type of operation

clients/algoliasearch-client-javascript/model/rankingInfo.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestFile } from './models';
21
import { RankingInfoMatchedGeoLocation } from './rankingInfoMatchedGeoLocation';
32

43
export class RankingInfo {

clients/algoliasearch-client-javascript/model/rankingInfoMatchedGeoLocation.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class RankingInfoMatchedGeoLocation {
42
/**
53
* Latitude of the matched location.

clients/algoliasearch-client-javascript/model/record.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { RequestFile } from './models';
21
import { HighlightResult } from './highlightResult';
32
import { RankingInfo } from './rankingInfo';
43
import { SnippetResult } from './snippetResult';
54

65
/**
76
* A single record
87
*/
9-
export class Record extends null<String, object> {
8+
// export class Record extends null<String, object>
9+
export class Record {
1010
/**
1111
* Unique identifier of the object
1212
*/
@@ -47,6 +47,6 @@ export class Record extends null<String, object> {
4747
];
4848

4949
static getAttributeTypeMap() {
50-
return super.getAttributeTypeMap().concat(Record.attributeTypeMap);
50+
return Record.attributeTypeMap;
5151
}
5252
}

clients/algoliasearch-client-javascript/model/saveObjectResponse.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class SaveObjectResponse {
42
'createdAt'?: string;
53
/**

clients/algoliasearch-client-javascript/model/searchParams.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class SearchParams {
42
/**
53
* The text to search in the index.

clients/algoliasearch-client-javascript/model/searchParamsString.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class SearchParamsString {
42
'params'?: string;
53

clients/algoliasearch-client-javascript/model/searchResponse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestFile } from './models';
21
import { Record } from './record';
32
import { SearchResponseFacetsStats } from './searchResponseFacetsStats';
43

clients/algoliasearch-client-javascript/model/searchResponseFacetsStats.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class SearchResponseFacetsStats {
42
/**
53
* The minimum value in the result set.

clients/algoliasearch-client-javascript/model/snippetResult.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { RequestFile } from './models';
2-
31
export class SnippetResult {
42
/**
53
* Markup text with occurrences highlighted.

clients/algoliasearch-client-javascript/utils/Requester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Requester {
6363

6464
req.on('error', (error) => {
6565
clearTimeout(connectTimeout);
66-
clearTimeout(responseTimeout as NodeJS.Timeout);
66+
clearTimeout(responseTimeout!);
6767
resolve({ status: 0, content: error.message, isTimedOut: false });
6868
});
6969

clients/algoliasearch-client-javascript/utils/Transporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class Transporter {
144144
method,
145145
url: serializeUrl(host, request.path, queryParameters),
146146
connectTimeout: getTimeout(timeoutsCount, this.timeouts.connect),
147-
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout as number),
147+
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout ?? this.timeouts.read),
148148
};
149149

150150
/**

clients/utils/javascript/Requester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Requester {
6363

6464
req.on('error', (error) => {
6565
clearTimeout(connectTimeout);
66-
clearTimeout(responseTimeout as NodeJS.Timeout);
66+
clearTimeout(responseTimeout!);
6767
resolve({ status: 0, content: error.message, isTimedOut: false });
6868
});
6969

clients/utils/javascript/Transporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class Transporter {
144144
method,
145145
url: serializeUrl(host, request.path, queryParameters),
146146
connectTimeout: getTimeout(timeoutsCount, this.timeouts.connect),
147-
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout as number),
147+
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout ?? this.timeouts.read),
148148
};
149149

150150
/**

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"playground/javascript/"
77
],
88
"scripts": {
9-
"build:spec": "yarn swagger-cli bundle openapi_spec/spec.yml --outfile dist/openapi.yml --type yaml",
9+
"build:spec": "yarn swagger-cli bundle specs/spec.yml --outfile dist/openapi.yml --type yaml",
10+
"build:spec:json": "yarn swagger-cli bundle specs/spec.yml --outfile dist/openapi.json --type json",
1011
"clean": "rm -rf **/dist **/build **/node_modules",
1112
"client:build-js": "cd clients/algoliasearch-client-javascript/ && yarn install && yarn build && cd ../../",
1213
"client:build": "yarn client:build-js",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
ALGOLIA_APPLICATION_ID=""
22
ALGOLIA_ADMIN_KEY=""
33
ALGOLIA_SEARCH_KEY=""
4+
5+
SEARCH_INDEX=""
6+
SEARCH_QUERY=""

playground/javascript/app.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { searchClient, HttpError } from 'algoliasearch-client-javascript';
1+
import { searchClient, ApiError } from 'algoliasearch-client-javascript';
22
import dotenv from 'dotenv';
33

4-
dotenv.config();
4+
dotenv.config({ path: '../.env' });
55

66
const appId = process.env.ALGOLIA_APPLICATION_ID || '**** APP_ID *****';
77
const apiKey = process.env.ALGOLIA_SEARCH_KEY || '**** SEARCH_API_KEY *****';
88

9+
const searchIndex = process.env.SEARCH_INDEX || 'test_index';
10+
const searchQuery = process.env.SEARCH_QUERY || 'test_query';
11+
912
// Init client with appId and apiKey
1013
const client = new searchClient(appId, apiKey);
1114

@@ -14,16 +17,16 @@ async function testMultiQueries() {
1417
const res = await client.multipleQueries({
1518
requests: [
1619
{
17-
indexName: 'docsearch',
18-
query: 'crawler',
20+
indexName: searchIndex,
21+
query: searchQuery,
1922
},
2023
],
2124
});
2225

2326
console.log(`[OK]`, res);
2427
} catch (e) {
25-
if (e instanceof HttpError) {
26-
return console.log(`[${e.statusCode} - ${e.response.statusMessage}]`, e.response);
28+
if (e instanceof ApiError) {
29+
return console.log(`[${e.status}] ${e.message}`, e.stackTrace);
2730
}
2831

2932
console.log('[ERROR]', e);
@@ -32,14 +35,14 @@ async function testMultiQueries() {
3235

3336
async function testSearch() {
3437
try {
35-
const res = await client.search('docsearch', {
36-
query: 'crawler',
38+
const res = await client.search(searchIndex, {
39+
query: searchQuery,
3740
});
3841

3942
console.log(`[OK]`, res);
4043
} catch (e) {
41-
if (e instanceof HttpError) {
42-
return console.log(`[${e.statusCode} - ${e.response.statusMessage}]`, e.response);
44+
if (e instanceof ApiError) {
45+
return console.log(`[${e.status}] ${e.message}`, e.stackTrace);
4346
}
4447

4548
console.log('[ERROR]', e);

specs/responses/BadRequest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ description: Bad request or request arguments
22
content:
33
application/json:
44
schema:
5-
$ref: '../schemas/Error.yml'
5+
$ref: '../schemas/ErrorBase.yml'

specs/responses/IndexNotFound.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ description: Index not found
22
content:
33
application/json:
44
schema:
5-
$ref: '../schemas/Error.yml'
5+
$ref: '../schemas/ErrorBase.yml'
File renamed without changes.

specs/schemas/Record.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ record:
1414
_rankingInfo:
1515
$ref: '#/rankingInfo'
1616
_distinctSeqID:
17-
type: number
17+
type: integer
1818

1919
# Props
2020
highlightResult:
@@ -74,10 +74,12 @@ rankingInfo:
7474
additionalProperties: false
7575
properties:
7676
lat:
77-
type: float
77+
type: number
78+
format: double
7879
description: 'Latitude of the matched location.'
7980
lng:
80-
type: float
81+
type: number
82+
format: double
8183
description: 'Longitude of the matched location.'
8284
distance:
8385
type: integer

specs/schemas/SearchParams.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,11 @@ searchParams:
215215
items:
216216
type: number
217217
description: Search inside a rectangular area (in geo coordinates).
218-
default: null
219218
insidePolygon:
220219
type: array
221220
items:
222221
type: number
223222
description: Search inside a polygon (in geo coordinates).
224-
default: null
225223
ignorePlurals:
226224
type: string
227225
description: Treats singular, plurals, and other forms of declensions as matching terms.

specs/schemas/SearchResponse.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ searchResponse:
101101
type: string
102102
description: The query string that will be searched, after normalization.
103103
processingTimeMS:
104-
type: number
104+
type: integer
105105
description: Time the server took to process the request, in milliseconds.
106106
example: 20
107107
query:

0 commit comments

Comments
 (0)