Skip to content

Commit

Permalink
still not compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Nov 17, 2021
1 parent 67d67f5 commit 10cc703
Show file tree
Hide file tree
Showing 26 changed files with 1,419 additions and 317 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2

[*.{mustache,ts,yml}]
charset = utf-8
155 changes: 50 additions & 105 deletions algolia-typescript-template/api-single.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import localVarRequest from 'request';
import http from 'http';
import { shuffle } from '../complement/helpers';
import { Transporter } from '../complement/Transporter';
import { Headers, Host, Request, RequestOptions } from '../complement/types';

{{#imports}}
import { {{classname}} } from '{{filename}}';
Expand All @@ -12,12 +14,6 @@ import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/model

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

let defaultBasePath = '{{{basePath}}}';

// ===============================================
// This file is autogenerated - Please do not edit
// ===============================================

{{#operations}}
{{#description}}
/**
Expand All @@ -33,9 +29,7 @@ export enum {{classname}}ApiKeys {
}

export class {{classname}} {
protected _basePath = defaultBasePath;
protected _defaultHeaders : any = {};
protected _useQuerystring : boolean = false;
private transporter: Transporter;
protected authentications = {
'default': <Authentication>new VoidAuth(),
Expand All @@ -59,35 +53,30 @@ export class {{classname}} {

protected interceptors: Interceptor[] = [];

constructor(appId: string, apiKey: string, basePath?: string) {
constructor(appId: string, apiKey: string) {
this.setApiKey(SearchApiApiKeys.appId, appId);
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
this.basePath = 'https://' + appId + '-1.algolianet.com';
if (basePath) {
this.basePath = basePath;
}
}

set useQuerystring(value: boolean) {
this._useQuerystring = value;
}

set basePath(basePath: string) {
this._basePath = basePath;
}

set defaultHeaders(defaultHeaders: any) {
this._defaultHeaders = defaultHeaders;
}

get defaultHeaders() {
return this._defaultHeaders;
}

get basePath() {
return this._basePath;
this.transporter = new Transporter({
hosts: ([
{ url: `${appId}-dsn.algolia.net`, accept: 'read', protocol: 'https' },
{ url: `${appId}.algolia.net`, accept: 'write', protocol: 'https' },
] as Host[]).concat(
shuffle([
{ url: `${appId}-1.algolianet.com`, accept: 'readWrite', protocol: 'https' },
{ url: `${appId}-2.algolianet.com`, accept: 'readWrite', protocol: 'https' },
{ url: `${appId}-3.algolianet.com`, accept: 'readWrite', protocol: 'https' },
])
),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded'
},
userAgent: 'Algolia for Javascript',
timeouts: {
connect: 2,
read: 5,
write: 30,
},
});
}

public setDefaultAuthentication(auth: Authentication) {
Expand Down Expand Up @@ -139,20 +128,20 @@ export class {{classname}} {
{{/allParams}}
*/
public async {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.IncomingMessage; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
const localVarPath = this.basePath + '{{{path}}}'{{#pathParams}}
const path = '{{{path}}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}};
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this._defaultHeaders);
let headers: Headers = {};
let queryParams: Record<string, string> = {};
{{#hasProduces}}
const produces = [{{#produces}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
localVarHeaderParams.Accept = 'application/json';
headers.Accept = 'application/json';
} else {
localVarHeaderParams.Accept = produces.join(',');
headers.Accept = produces.join(',');
}
{{/hasProduces}}
let localVarFormParams: any = {};
let formParams: Record<string, string> = {};

{{#allParams}}
{{#required}}
Expand All @@ -165,103 +154,59 @@ export class {{classname}} {
{{/allParams}}
{{#queryParams}}
if ({{paramName}} !== undefined) {
localVarQueryParameters['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
queryParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
}

{{/queryParams}}
{{#headerParams}}
localVarHeaderParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
headers['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
{{/headerParams}}
(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;
headers = {...headers, ...options.headers};

{{#formParams}}
if ({{paramName}} !== undefined) {
{{#isFile}}
localVarFormParams['{{baseName}}'] = {{paramName}};
{{/isFile}}
{{^isFile}}
localVarFormParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
{{/isFile}}
}
{{#isFile}}
localVarUseFormData = true;
{{/isFile}}

{{/formParams}}
let localVarRequestOptions: localVarRequest.Options = {
const request: Request = {
method: '{{httpMethod}}',
qs: localVarQueryParameters,
headers: localVarHeaderParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
{{^isResponseFile}}
json: true,
{{/isResponseFile}}
{{#isResponseFile}}
encoding: null,
{{/isResponseFile}}
url: path,
headers,
{{#bodyParam}}
body: ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}")
data: ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}")
{{/bodyParam}}
};

const requestOptions: RequestOptions = {};

let authenticationPromise = Promise.resolve();
{{#authMethods}}
{{#isApiKey}}
if (this.authentications.{{name}}.apiKey) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(requestOptions));
}
{{/isApiKey}}
{{#isBasicBasic}}
if (this.authentications.{{name}}.username && this.authentications.{{name}}.password) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(requestOptions));
}
{{/isBasicBasic}}
{{#isBasicBearer}}
if (this.authentications.{{name}}.accessToken) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(requestOptions));
}
{{/isBasicBearer}}
{{#isOAuth}}
if (this.authentications.{{name}}.accessToken) {
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(localVarRequestOptions));
authenticationPromise = authenticationPromise.then(() => this.authentications.{{name}}.applyToRequest(requestOptions));
}
{{/isOAuth}}
{{/authMethods}}
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(requestOptions));

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

return interceptorPromise.then(() => {
if (Object.keys(localVarFormParams).length) {
if (localVarUseFormData) {
(<any>localVarRequestOptions).formData = localVarFormParams;
} else {
localVarRequestOptions.form = localVarFormParams;
}
}
return new Promise<{ response: http.IncomingMessage; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>((resolve, reject) => {
localVarRequest(localVarRequestOptions, (error, response, body) => {
if (error) {
reject(error);
} else {
{{#returnType}}
body = ObjectSerializer.deserialize(body, "{{{returnType}}}");
{{/returnType}}
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
resolve({ response: response, body: body });
} else {
reject(new HttpError(response, body, response.statusCode));
}
}
});
});
});
await interceptorPromise;

return transporter.retryableRequest(request, requestOptions);
}
{{/operation}}
}
Expand Down
16 changes: 8 additions & 8 deletions algolia-typescript-template/models.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#generateApis}}
import localVarRequest from 'request';
import type { RequestOptions } from '../complement/types';
{{/generateApis}}

{{#models}}
Expand Down Expand Up @@ -182,14 +182,14 @@ export interface Authentication {
/**
* Apply authentication settings to header and query params.
*/
applyToRequest(requestOptions: localVarRequest.Options): Promise<void> | void;
applyToRequest(requestOptions: RequestOptions): Promise<void> | void;
}
export class HttpBasicAuth implements Authentication {
public username: string = '';
public password: string = '';
applyToRequest(requestOptions: localVarRequest.Options): void {
applyToRequest(requestOptions: RequestOptions): void {
requestOptions.auth = {
username: this.username, password: this.password
}
Expand All @@ -199,7 +199,7 @@ export class HttpBasicAuth implements Authentication {
export class HttpBearerAuth implements Authentication {
public accessToken: string | (() => string) = '';
applyToRequest(requestOptions: localVarRequest.Options): void {
applyToRequest(requestOptions: RequestOptions): void {
if (requestOptions && requestOptions.headers) {
const accessToken = typeof this.accessToken === 'function'
? this.accessToken()
Expand All @@ -215,7 +215,7 @@ export class ApiKeyAuth implements Authentication {
constructor(private location: string, private paramName: string) {
}
applyToRequest(requestOptions: localVarRequest.Options): void {
applyToRequest(requestOptions: RequestOptions): void {
if (this.location == "query") {
(<any>requestOptions.qs)[this.paramName] = this.apiKey;
} else if (this.location == "header" && requestOptions && requestOptions.headers) {
Expand All @@ -234,7 +234,7 @@ export class ApiKeyAuth implements Authentication {
export class OAuth implements Authentication {
public accessToken: string = '';
applyToRequest(requestOptions: localVarRequest.Options): void {
applyToRequest(requestOptions: RequestOptions): void {
if (requestOptions && requestOptions.headers) {
requestOptions.headers["Authorization"] = "Bearer " + this.accessToken;
}
Expand All @@ -245,10 +245,10 @@ export class VoidAuth implements Authentication {
public username: string = '';
public password: string = '';
applyToRequest(_: localVarRequest.Options): void {
applyToRequest(_: RequestOptions): void {
// Do nothing
}
}
export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise<void> | void);
export type Interceptor = (requestOptions: RequestOptions) => (Promise<void> | void);
{{/generateApis}}
21 changes: 21 additions & 0 deletions complement/Cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface Cache {
/**
* Gets the value of the given `key`.
*/
get: <TValue>(key: object | string, defaultValue: () => Promise<TValue>) => Promise<TValue>;

/**
* Sets the given value with the given `key`.
*/
set: <TValue>(key: object | string, value: TValue) => Promise<TValue>;

/**
* Deletes the given `key`.
*/
delete: (key: object | string) => Promise<void>;

/**
* Clears the cache.
*/
clear: () => Promise<void>;
}
27 changes: 27 additions & 0 deletions complement/MemoryCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Cache } from './Cache';

export class MemoryCache implements Cache {
private cache: Record<string, any> = {};

async get<TValue>(key: object | string, defaultValue: () => Promise<TValue>): Promise<TValue> {
const keyAsString = JSON.stringify(key);

if (keyAsString in this.cache) {
return Promise.resolve(this.cache[keyAsString]);
}
return await defaultValue();
}

async set<TValue>(key: object | string, value: TValue): Promise<TValue> {
this.cache[JSON.stringify(key)] = value;
return value;
}

async delete(key: object | string): Promise<void> {
delete this.cache[JSON.stringify(key)];
}

async clear(): Promise<void> {
this.cache = {};
}
}
Loading

0 comments on commit 10cc703

Please sign in to comment.