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

fix: clean code generation and types #12

Merged
merged 6 commits into from
Nov 22, 2021
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ yarn client:build

# Testing clients

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

## JavaScript

```bash
cd playground/javascript && yarn start
yarn playground:js
```

# Troubleshooting
Expand Down
12 changes: 2 additions & 10 deletions clients/algoliasearch-client-javascript/client-search/apis.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
export * from './searchApi';
import { SearchApi } from './searchApi';
import * as http from 'http';

export class HttpError extends Error {
constructor(public response: http.IncomingMessage, public body: any, public statusCode?: number) {
super('HTTP request failed');
this.name = 'HttpError';
}
}

export { RequestFile } from '../model/models';

export class searchClient extends SearchApi {}

export * from '../utils/errors';

export const APIS = [SearchApi];
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Headers, Host, Request, RequestOptions } from '../utils/types';

import { BatchObject } from '../model/batchObject';
import { BatchResponse } from '../model/batchResponse';
import { ErrorBase } from '../model/errorBase';
import { MultipleQueriesObject } from '../model/multipleQueriesObject';
import { MultipleQueriesResponse } from '../model/multipleQueriesResponse';
import { SaveObjectResponse } from '../model/saveObjectResponse';
Expand All @@ -15,8 +16,6 @@ import { SearchResponse } from '../model/searchResponse';
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
import { HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';

import { HttpError, RequestFile } from './apis';

export enum SearchApiApiKeys {
apiKey,
appId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RequestFile } from './models';
import { Operation } from './operation';

export class BatchObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class BatchResponse {
/**
* taskID of the indexing task to wait for.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { RequestFile } from './models';

/**
* Error
*/
export class ModelError extends null<String, object> {
// export class ErrorBase extends null<String, object>
export class ErrorBase {
'message'?: string;

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

static getAttributeTypeMap() {
return super.getAttributeTypeMap().concat(ModelError.attributeTypeMap);
return ErrorBase.attributeTypeMap;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class HighlightResult {
/**
* Markup text with occurrences highlighted.
Expand Down
18 changes: 3 additions & 15 deletions clients/algoliasearch-client-javascript/model/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { RequestOptions } from '../utils/types';

export * from './batchObject';
export * from './batchResponse';
export * from './errorBase';
export * from './highlightResult';
export * from './modelError';
export * from './multipleQueries';
export * from './multipleQueriesObject';
export * from './multipleQueriesResponse';
Expand All @@ -18,22 +18,10 @@ export * from './searchResponse';
export * from './searchResponseFacetsStats';
export * from './snippetResult';

import * as fs from 'fs';

export interface RequestDetailedFile {
value: Buffer;
options?: {
filename?: string;
contentType?: string;
};
}

export type RequestFile = string | Buffer | fs.ReadStream | RequestDetailedFile;

import { BatchObject } from './batchObject';
import { BatchResponse } from './batchResponse';
import { ErrorBase } from './errorBase';
import { HighlightResult } from './highlightResult';
import { ModelError } from './modelError';
import { MultipleQueries } from './multipleQueries';
import { MultipleQueriesObject } from './multipleQueriesObject';
import { MultipleQueriesResponse } from './multipleQueriesResponse';
Expand Down Expand Up @@ -67,8 +55,8 @@ let enumsMap: { [index: string]: any } = {
let typeMap: { [index: string]: any } = {
BatchObject: BatchObject,
BatchResponse: BatchResponse,
ErrorBase: ErrorBase,
HighlightResult: HighlightResult,
ModelError: ModelError,
MultipleQueries: MultipleQueries,
MultipleQueriesObject: MultipleQueriesObject,
MultipleQueriesResponse: MultipleQueriesResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class MultipleQueries {
/**
* The Algolia index name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RequestFile } from './models';
import { MultipleQueries } from './multipleQueries';

export class MultipleQueriesObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RequestFile } from './models';
import { SearchResponse } from './searchResponse';

export class MultipleQueriesResponse {
Expand Down
2 changes: 0 additions & 2 deletions clients/algoliasearch-client-javascript/model/operation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class Operation {
/**
* type of operation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RequestFile } from './models';
import { RankingInfoMatchedGeoLocation } from './rankingInfoMatchedGeoLocation';

export class RankingInfo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class RankingInfoMatchedGeoLocation {
/**
* Latitude of the matched location.
Expand Down
6 changes: 3 additions & 3 deletions clients/algoliasearch-client-javascript/model/record.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { RequestFile } from './models';
import { HighlightResult } from './highlightResult';
import { RankingInfo } from './rankingInfo';
import { SnippetResult } from './snippetResult';

/**
* A single record
*/
export class Record extends null<String, object> {
// export class Record extends null<String, object>
export class Record {
/**
* Unique identifier of the object
*/
Expand Down Expand Up @@ -47,6 +47,6 @@ export class Record extends null<String, object> {
];

static getAttributeTypeMap() {
return super.getAttributeTypeMap().concat(Record.attributeTypeMap);
return Record.attributeTypeMap;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class SaveObjectResponse {
'createdAt'?: string;
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class SearchParams {
/**
* The text to search in the index.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class SearchParamsString {
'params'?: string;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RequestFile } from './models';
import { Record } from './record';
import { SearchResponseFacetsStats } from './searchResponseFacetsStats';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class SearchResponseFacetsStats {
/**
* The minimum value in the result set.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestFile } from './models';

export class SnippetResult {
/**
* Markup text with occurrences highlighted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Requester {

req.on('error', (error) => {
clearTimeout(connectTimeout);
clearTimeout(responseTimeout as NodeJS.Timeout);
clearTimeout(responseTimeout!);
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
resolve({ status: 0, content: error.message, isTimedOut: false });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class Transporter {
method,
url: serializeUrl(host, request.path, queryParameters),
connectTimeout: getTimeout(timeoutsCount, this.timeouts.connect),
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout as number),
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout ?? this.timeouts.read),
};

/**
Expand Down
2 changes: 1 addition & 1 deletion clients/utils/javascript/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Requester {

req.on('error', (error) => {
clearTimeout(connectTimeout);
clearTimeout(responseTimeout as NodeJS.Timeout);
clearTimeout(responseTimeout!);
resolve({ status: 0, content: error.message, isTimedOut: false });
});

Expand Down
2 changes: 1 addition & 1 deletion clients/utils/javascript/Transporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class Transporter {
method,
url: serializeUrl(host, request.path, queryParameters),
connectTimeout: getTimeout(timeoutsCount, this.timeouts.connect),
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout as number),
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout ?? this.timeouts.read),
};

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"playground/javascript/"
],
"scripts": {
"build:spec": "yarn swagger-cli bundle openapi_spec/spec.yml --outfile dist/openapi.yml --type yaml",
"build:spec": "yarn swagger-cli bundle specs/spec.yml --outfile dist/openapi.yml --type yaml",
"build:spec:json": "yarn swagger-cli bundle specs/spec.yml --outfile dist/openapi.json --type json",
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
"clean": "rm -rf **/dist **/build **/node_modules",
"client:build-js": "cd clients/algoliasearch-client-javascript/ && yarn install && yarn build && cd ../../",
"client:build": "yarn client:build-js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ALGOLIA_APPLICATION_ID=""
ALGOLIA_ADMIN_KEY=""
ALGOLIA_SEARCH_KEY=""

SEARCH_INDEX=""
SEARCH_QUERY=""
23 changes: 13 additions & 10 deletions playground/javascript/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { searchClient, HttpError } from 'algoliasearch-client-javascript';
import { searchClient, ApiError } from 'algoliasearch-client-javascript';
import dotenv from 'dotenv';

dotenv.config();
dotenv.config({ path: '../.env' });

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

const searchIndex = process.env.SEARCH_INDEX || 'test_index';
const searchQuery = process.env.SEARCH_QUERY || 'test_query';

// Init client with appId and apiKey
const client = new searchClient(appId, apiKey);

Expand All @@ -14,16 +17,16 @@ async function testMultiQueries() {
const res = await client.multipleQueries({
requests: [
{
indexName: 'docsearch',
query: 'crawler',
indexName: searchIndex,
query: searchQuery,
},
],
});

console.log(`[OK]`, res);
} catch (e) {
if (e instanceof HttpError) {
return console.log(`[${e.statusCode} - ${e.response.statusMessage}]`, e.response);
if (e instanceof ApiError) {
return console.log(`[${e.status}] ${e.message}`, e.stackTrace);
}

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

async function testSearch() {
try {
const res = await client.search('docsearch', {
query: 'crawler',
const res = await client.search(searchIndex, {
query: searchQuery,
});

console.log(`[OK]`, res);
} catch (e) {
if (e instanceof HttpError) {
return console.log(`[${e.statusCode} - ${e.response.statusMessage}]`, e.response);
if (e instanceof ApiError) {
return console.log(`[${e.status}] ${e.message}`, e.stackTrace);
}

console.log('[ERROR]', e);
Expand Down
2 changes: 1 addition & 1 deletion specs/responses/BadRequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ description: Bad request or request arguments
content:
application/json:
schema:
$ref: '../schemas/Error.yml'
$ref: '../schemas/ErrorBase.yml'
2 changes: 1 addition & 1 deletion specs/responses/IndexNotFound.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ description: Index not found
content:
application/json:
schema:
$ref: '../schemas/Error.yml'
$ref: '../schemas/ErrorBase.yml'
File renamed without changes.
8 changes: 5 additions & 3 deletions specs/schemas/Record.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ record:
_rankingInfo:
$ref: '#/rankingInfo'
_distinctSeqID:
type: number
type: integer

# Props
highlightResult:
Expand Down Expand Up @@ -74,10 +74,12 @@ rankingInfo:
additionalProperties: false
properties:
lat:
type: float
type: number
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
format: double
description: 'Latitude of the matched location.'
lng:
type: float
type: number
format: double
description: 'Longitude of the matched location.'
distance:
type: integer
Expand Down
2 changes: 0 additions & 2 deletions specs/schemas/SearchParams.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,11 @@ searchParams:
items:
type: number
description: Search inside a rectangular area (in geo coordinates).
default: null
insidePolygon:
type: array
items:
type: number
description: Search inside a polygon (in geo coordinates).
default: null
ignorePlurals:
type: string
description: Treats singular, plurals, and other forms of declensions as matching terms.
Expand Down
2 changes: 1 addition & 1 deletion specs/schemas/SearchResponse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ searchResponse:
type: string
description: The query string that will be searched, after normalization.
processingTimeMS:
type: number
type: integer
description: Time the server took to process the request, in milliseconds.
example: 20
query:
Expand Down
Loading