[@types/algoliasearch] Add proper types for ApiKey (#38158)

* [@types/algoliasearch] add proper types for ApiKey

* lint

* lint

* revert some prettier changes
This commit is contained in:
Samuel Bodin
2019-09-12 09:03:08 -07:00
committed by Andrew Branch
parent 5a51040804
commit 21cf492e4f
2 changed files with 227 additions and 172 deletions
+147 -144
View File
@@ -1,162 +1,167 @@
import * as algoliasearch from 'algoliasearch';
import {
ClientOptions,
SynonymOption,
ApiKeyOptions,
SearchSynonymOptions,
SecuredApiOptions,
Index,
Response,
IndexSettings,
QueryParameters,
Client,
ClientOptions,
SynonymOption,
ApiKeyOptions,
SearchSynonymOptions,
SecuredApiOptions,
Index,
Response,
IndexSettings,
QueryParameters,
ApiKey,
MultiResponse,
Client,
} from 'algoliasearch';
import * as algoliasearchLite from 'algoliasearch/lite';
let _algoliaResponse: Response = {
hits: [{}, {}],
page: 0,
nbHits: 12,
nbPages: 6,
hitsPerPage: 2,
processingTimeMS: 32,
query: '',
params: '',
index: '',
exhaustiveFacetsCount: true,
exhaustiveNbHits: false,
hits: [{}, {}],
page: 0,
nbHits: 12,
nbPages: 6,
hitsPerPage: 2,
processingTimeMS: 32,
query: '',
params: '',
index: '',
exhaustiveFacetsCount: true,
exhaustiveNbHits: false,
};
let _clientOptions: ClientOptions = {
timeout: 12,
protocol: '',
httpAgent: '',
timeout: 12,
protocol: '',
httpAgent: '',
};
let _synonymOption: SynonymOption = {
forwardToReplicas: false,
replaceExistingSynonyms: false,
forwardToReplicas: false,
replaceExistingSynonyms: false,
};
let _algoliaApiKeyOptions: ApiKeyOptions = {
validity: 0,
maxQueriesPerIPPerHour: 0,
indexes: [''],
queryParameters: { attributesToRetrieve: ['algolia'] },
description: '',
validity: 0,
maxQueriesPerIPPerHour: 0,
indexes: [''],
queryParameters: { attributesToRetrieve: ['algolia'] },
description: '',
};
let _searchSynonymOptions: SearchSynonymOptions = {
query: '',
page: 0,
type: '',
hitsPerPage: 0,
query: '',
page: 0,
type: '',
hitsPerPage: 0,
};
let _algoliaSecuredApiOptions: SecuredApiOptions = {
filters: '',
validUntil: 0,
restrictIndices: '',
userToken: '',
filters: '',
validUntil: 0,
restrictIndices: '',
userToken: '',
};
let _algoliaIndexSettings: IndexSettings = {
attributesToIndex: [''],
attributesForFaceting: [''],
unretrievableAttributes: [''],
attributesToRetrieve: [''],
ranking: [''],
customRanking: [''],
replicas: [''],
maxValuesPerFacet: 100,
attributesToHighlight: [''],
attributesToSnippet: [''],
highlightPreTag: '',
highlightPostTag: '',
snippetEllipsisText: '',
restrictHighlightAndSnippetArrays: false,
hitsPerPage: 0,
minWordSizefor1Typo: 0,
minWordSizefor2Typos: 0,
typoTolerance: false,
allowTyposOnNumericTokens: false,
ignorePlurals: false,
disableTypoToleranceOnAttributes: '',
separatorsToIndex: '',
queryType: 'prefixAll',
removeWordsIfNoResults: '',
advancedSyntax: false,
optionalWords: [''],
removeStopWords: [''],
disablePrefixOnAttributes: [''],
disableExactOnAttributes: [''],
exactOnSingleWordQuery: '',
alternativesAsExact: ['ignorePlurals'],
attributeForDistinct: '',
distinct: false,
numericAttributesToIndex: [''],
allowCompressionOfIntegerArray: false,
altCorrections: [{}],
minProximity: 0,
placeholders: { '': [''] },
camelCaseAttributes: [''],
sortFacetValuesBy: 'count',
queryLanguages: ['fr', 'es'],
attributesToIndex: [''],
attributesForFaceting: [''],
unretrievableAttributes: [''],
attributesToRetrieve: [''],
ranking: [''],
customRanking: [''],
replicas: [''],
maxValuesPerFacet: 100,
attributesToHighlight: [''],
attributesToSnippet: [''],
highlightPreTag: '',
highlightPostTag: '',
snippetEllipsisText: '',
restrictHighlightAndSnippetArrays: false,
hitsPerPage: 0,
minWordSizefor1Typo: 0,
minWordSizefor2Typos: 0,
typoTolerance: false,
allowTyposOnNumericTokens: false,
ignorePlurals: false,
disableTypoToleranceOnAttributes: '',
separatorsToIndex: '',
queryType: 'prefixAll',
removeWordsIfNoResults: '',
advancedSyntax: false,
optionalWords: [''],
removeStopWords: [''],
disablePrefixOnAttributes: [''],
disableExactOnAttributes: [''],
exactOnSingleWordQuery: '',
alternativesAsExact: ['ignorePlurals'],
attributeForDistinct: '',
distinct: false,
numericAttributesToIndex: [''],
allowCompressionOfIntegerArray: false,
altCorrections: [{}],
minProximity: 0,
placeholders: { '': [''] },
camelCaseAttributes: [''],
};
let _algoliaQueryParameters: QueryParameters = {
query: '',
filters: '',
attributesToRetrieve: [''],
restrictSearchableAttributes: [''],
facets: [''],
facetingAfterDistinct: true,
maxValuesPerFacet: 2,
attributesToHighlight: [''],
attributesToSnippet: [''],
highlightPreTag: '',
highlightPostTag: '',
snippetEllipsisText: '',
restrictHighlightAndSnippetArrays: false,
hitsPerPage: 0,
page: 0,
offset: 0,
length: 0,
minWordSizefor1Typo: 0,
minWordSizefor2Typos: 0,
typoTolerance: false,
allowTyposOnNumericTokens: false,
ignorePlurals: false,
disableTypoToleranceOnAttributes: [''],
aroundLatLng: '',
aroundLatLngViaIP: true,
aroundRadius: 0,
aroundPrecision: 0,
minimumAroundRadius: 0,
insideBoundingBox: [[0]],
queryType: 'prefixAll',
insidePolygon: [[0]],
removeWordsIfNoResults: 'firstWords',
advancedSyntax: false,
optionalWords: [''],
removeStopWords: [''],
disableExactOnAttributes: [''],
exactOnSingleWordQuery: 'attribute',
alternativesAsExact: ['ignorePlurals'],
distinct: 0,
getRankingInfo: false,
numericAttributesToIndex: [''],
numericAttributesForFiltering: [''],
numericFilters: [''],
tagFilters: [''],
facetFilters: ['', ['']],
analytics: false,
clickAnalytics: true,
analyticsTags: [''],
synonyms: true,
replaceSynonymsInHighlight: false,
minProximity: 0,
sortFacetValuesBy: 'alpha',
query: '',
filters: '',
attributesToRetrieve: [''],
restrictSearchableAttributes: [''],
facets: [''],
facetingAfterDistinct: true,
maxValuesPerFacet: 2,
attributesToHighlight: [''],
attributesToSnippet: [''],
highlightPreTag: '',
highlightPostTag: '',
snippetEllipsisText: '',
restrictHighlightAndSnippetArrays: false,
hitsPerPage: 0,
page: 0,
offset: 0,
length: 0,
minWordSizefor1Typo: 0,
minWordSizefor2Typos: 0,
typoTolerance: false,
allowTyposOnNumericTokens: false,
ignorePlurals: false,
disableTypoToleranceOnAttributes: [''],
aroundLatLng: '',
aroundLatLngViaIP: true,
aroundRadius: 0,
aroundPrecision: 0,
minimumAroundRadius: 0,
insideBoundingBox: [[0]],
queryType: 'prefixAll',
insidePolygon: [[0]],
removeWordsIfNoResults: 'firstWords',
advancedSyntax: false,
optionalWords: [''],
removeStopWords: [''],
disableExactOnAttributes: [''],
exactOnSingleWordQuery: 'attribute',
alternativesAsExact: ['ignorePlurals'],
distinct: 0,
getRankingInfo: false,
numericAttributesToIndex: [''],
numericAttributesForFiltering: [''],
numericFilters: [''],
tagFilters: [''],
facetFilters: [''],
analytics: false,
analyticsTags: [''],
synonyms: true,
replaceSynonymsInHighlight: false,
minProximity: 0,
};
let _apiKey: ApiKey = {
value: '0eb3e6308abccdf9b67d70ddacb418b4',
createdAt: 1513462891,
acl: ['search'],
validity: 0,
};
let client: Client = algoliasearch('', '');
@@ -191,21 +196,21 @@ client.copyIndex('from', 'to', ['synonyms', 'rules'], () => {});
const browser = index.browseAll();
index.browseAll('query');
index.browseAll('', {
filters: 'dog',
filters: 'dog',
});
let hits: Object[] = [];
browser.on('result', function onResult(content) {
hits = hits.concat(content.hits);
hits = hits.concat(content.hits);
});
browser.on('end', function onEnd() {
const _message = `We got ${hits.length} hits`;
const _message = `We got ${hits.length} hits`;
});
browser.on('error', function onError(err) {
throw err;
throw err;
});
browser.stop();
@@ -213,20 +218,18 @@ browser.stop();
index.setSettings({ hitsPerPage: 10 }, () => {});
index.setSettings({ hitsPerPage: 10 }, { forwardToReplicas: true }, () => {});
index.setSettings({ hitsPerPage: 10 }).then(() => {});
index
.setSettings({ hitsPerPage: 10 }, { forwardToReplicas: true })
.then(() => {});
index.setSettings({ hitsPerPage: 10 }, { forwardToReplicas: true }).then(() => {});
index.browse('', {
advancedSyntax: false,
attributesToRetrieve: ['dogs'],
advancedSyntax: false,
attributesToRetrieve: ['dogs'],
});
client.copyIndex('from', 'to', ['settings']).then(() => {});
client.copyIndex('from', 'to', ['synonyms', 'rules'], () => {});
const liteClient: algoliasearchLite.Client = algoliasearchLite('', '');
liteClient.search([], (err: Error, res: algoliasearch.MultiResponse) => {});
liteClient.search([]).then((res: algoliasearch.MultiResponse) => {});
liteClient.search([], (err: Error, res: MultiResponse) => {});
liteClient.search([]).then((res: MultiResponse) => {});
type Res = { zipzop: true };
liteClient.search<Res>([]).then((res: algoliasearch.MultiResponse<Res>) => {});
liteClient.search<Res>([]).then((res: MultiResponse<Res>) => {});
+80 -28
View File
@@ -10,6 +10,7 @@
// Alexandre Deve <https://github.com/adeve>
// Dan Grayson <https://github.com/dan-grayson>
// Peter Esenwa <https://github.com/PeterEsenwa>
// Samuel Bodin <https://github.com/bodinsamuel>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
@@ -139,34 +140,40 @@ declare namespace algoliasearch {
/**
* Lists global API Keys
*/
listApiKeys(cb: (err: Error, res: any) => void): void;
listApiKeys(cb: (err: Error, res: ApiKey[]) => void): void;
/**
* Lists global API Keys
*/
listApiKeys(): Promise<any>;
listApiKeys(): Promise<ApiKey[]>;
/**
* Add global API Keys
*/
addApiKey(scopes: string[], cb: (err: Error, res: Task) => void): void;
addApiKey(
scopes: string[],
cb: (err: Error, res: AddApiKeyTask) => void
): void;
/**
* Add global API Key
*/
addApiKey(
scopes: string[],
options: ApiKeyOptions,
cb: (err: Error, res: Task) => void
cb: (err: Error, res: AddApiKeyTask) => void
): void;
/**
* Add global API Keys
*/
addApiKey(scopes: string[], options?: ApiKeyOptions): Promise<Task>;
addApiKey(
scopes: string[],
options?: ApiKeyOptions
): Promise<AddApiKeyTask>;
/**
* Update global API key
*/
updateApiKey(
key: string,
scopes: string[],
cb: (err: Error, res: Task) => void
cb: (err: Error, res: UpdateApiKeyTask) => void
): void;
/**
* Update global API key
@@ -175,7 +182,7 @@ declare namespace algoliasearch {
key: string,
scopes: string[],
options: ApiKeyOptions,
cb: (err: Error, res: Task) => void
cb: (err: Error, res: UpdateApiKeyTask) => void
): void;
/**
* Update global API key
@@ -184,23 +191,26 @@ declare namespace algoliasearch {
key: string,
scopes: string[],
options?: ApiKeyOptions
): Promise<Task>;
): Promise<UpdateApiKeyTask>;
/**
* Gets the rights of a global key
*/
getApiKey(key: string, cb: (err: Error, res: any) => void): void;
getApiKey(key: string, cb: (err: Error, res: ApiKey) => void): void;
/**
* Gets the rights of a global key
*/
getApiKey(key: string): Promise<any>;
getApiKey(key: string): Promise<ApiKey>;
/**
* Deletes a global key
*/
deleteApiKey(key: string, cb: (err: Error, res: Task) => void): void;
deleteApiKey(
key: string,
cb: (err: Error, res: DeleteApiKeyTask) => void
): void;
/**
* Deletes a global key
*/
deleteApiKey(key: string): Promise<Task>;
deleteApiKey(key: string): Promise<DeleteApiKeyTask>;
/**
* Get 1000 last events
*/
@@ -424,18 +434,21 @@ declare namespace algoliasearch {
/**
* List index user keys
*/
listApiKeys(cb: (err: Error, res: any) => void): void;
listApiKeys(cb: (err: Error, res: ApiKey[]) => void): void;
/**
* Add key for this index
*/
addApiKey(scopes: string[], cb: (err: Error, res: Task) => void): void;
addApiKey(
scopes: string[],
cb: (err: Error, res: AddApiKeyTask) => void
): void;
/**
* Add key for this index
*/
addApiKey(
scopes: string[],
options: ApiKeyOptions,
cb: (err: Error, res: Task) => void
cb: (err: Error, res: AddApiKeyTask) => void
): void;
/**
* Update a key for this index
@@ -443,7 +456,7 @@ declare namespace algoliasearch {
updateApiKey(
key: string,
scopes: string[],
cb: (err: Error, res: Task) => void
cb: (err: Error, res: UpdateApiKeyTask) => void
): void;
/**
* Update a key for this index
@@ -452,16 +465,19 @@ declare namespace algoliasearch {
key: string,
scopes: string[],
options: ApiKeyOptions,
cb: (err: Error, res: Task) => void
cb: (err: Error, res: UpdateApiKeyTask) => void
): void;
/**
* Gets the rights of an index specific key
*/
getApiKey(key: string, cb: (err: Error, res: any) => void): void;
getApiKey(key: string, cb: (err: Error, res: ApiKey) => void): void;
/**
* Deletes an index specific key
*/
deleteApiKey(key: string, cb: (err: Error, res: Task) => void): void;
deleteApiKey(
key: string,
cb: (err: Error, res: DeleteApiKeyTask) => void
): void;
/**
* Gets specific attributes from an object
*/
@@ -630,15 +646,18 @@ declare namespace algoliasearch {
/**
* List index user keys
*/
listApiKeys(): Promise<any>;
listApiKeys(): Promise<ApiKey[]>;
/**
* Add key for this index
*/
addApiKey(scopes: string[], options?: ApiKeyOptions): Promise<Task>;
addApiKey(
scopes: string[],
options?: ApiKeyOptions
): Promise<AddApiKeyTask>;
/**
* Update a key for this index
*/
updateApiKey(key: string, scopes: string[]): Promise<Task>;
updateApiKey(key: string, scopes: string[]): Promise<UpdateApiKeyTask>;
/**
* Update a key for this index
*/
@@ -646,15 +665,15 @@ declare namespace algoliasearch {
key: string,
scopes: string[],
options: ApiKeyOptions
): Promise<Task>;
): Promise<UpdateApiKeyTask>;
/**
* Gets the rights of an index specific key
*/
getApiKey(key: string): Promise<any>;
getApiKey(key: string): Promise<ApiKey>;
/**
* Deletes an index specific key
*/
deleteApiKey(key: string): Promise<Task>;
deleteApiKey(key: string): Promise<DeleteApiKeyTask>;
}
/**
* Interface describing available options when initializing a client
@@ -1356,8 +1375,41 @@ declare namespace algoliasearch {
}
interface TaskStatus {
status: 'published' | 'notPublished',
pendingTask: boolean,
status: 'published' | 'notPublished';
pendingTask: boolean;
}
interface ApiKey {
value: string;
createdAt: number;
acl: (
| 'search'
| 'browse'
| 'addObject'
| 'deleteObject'
| 'deleteIndex'
| 'settings'
| 'editSettings'
| 'analytics'
| 'listIndexes'
| 'logs'
| 'seeUnretrievableAttributes')[];
validity: number;
description?: string;
}
interface AddApiKeyTask {
key: string;
createdAt: string;
}
interface UpdateApiKeyTask {
key: string;
updatedAt: string;
}
interface DeleteApiKeyTask {
deletedAt: string;
}
interface IndexSettings {
@@ -1689,7 +1741,7 @@ declare namespace algoliasearch {
* If a search encounters an index that is being A/B tested, abTestVariantID
* reports the variant ID of the index used (note, this is the ID not the name).
* The variant ID is the position in the array of variants (starting at 1).
*
*
* For example, abTestVariantID=1 is variant A (the main index), abTestVariantID=2
* is variant B (the replica you chose when creating the A/B test , or the queries
* with the changed query parameters if the A/B test is based on query parameters).