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

Add pagination system on resources for v0.28.0 #1269

Merged
merged 11 commits into from
Jun 27, 2022
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ If you want to know more about the development workflow or want to contribute, p

- [Get Documents](https://docs.meilisearch.com/reference/api/documents.html#get-documents):

`index.getDocuments(params: DocumentsParams): Promise<Result<Documents<T>>>`
`index.getDocuments(parameters: DocumentsQuery = {}): Promise<DocumentsResults<T>>>`

- [Get one document](https://docs.meilisearch.com/reference/api/documents.html#get-one-document):

Expand All @@ -415,15 +415,15 @@ If you want to know more about the development workflow or want to contribute, p

- [Get all tasks](https://docs.meilisearch.com/reference/api/tasks.html#get-all-tasks)

`client.getTasks(): Promise<Result<Task[]>>`
`client.getTasks(parameters: TasksQuery): Promise<TasksResults>`

- [Get one task](https://docs.meilisearch.com/reference/api/tasks.html#get-task)

`client.getTask(uid: number): Promise<Task>`

- [Get all tasks of an index](https://docs.meilisearch.com/reference/api/tasks.html#get-all-tasks-by-index)

`index.getTasks(): Promise<Result<Task[]>>`
`index.getTasks(parameters: TasksQuery): Promise<TasksResults>`

- [Get one task of an index](https://docs.meilisearch.com/reference/api/tasks.html#get-task)

Expand All @@ -446,11 +446,11 @@ If you want to know more about the development workflow or want to contribute, p

- [Get all indexes as Index instances](https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes):

`client.getIndexes(): Promise<Result<Index[]>>`
`client.getIndexes(parameters: IndexesQuery): Promise<IndexesResults<Index[]>>`

- [Get all indexes](https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes):

`client.getRawIndexes(): Promise<Result<IndexResponse[]>>`
`client.getRawIndexes(parameters: IndexesQuery): Promise<IndexesResults<IndexObject[]>>`

- [Create a new index](https://docs.meilisearch.com/reference/api/indexes.html#create-an-index):

Expand All @@ -464,10 +464,10 @@ If you want to know more about the development workflow or want to contribute, p
`client.getIndex<T>(uid: string): Promise<Index<T>>`

- [Get the raw index JSON response from Meilisearch](https://docs.meilisearch.com/reference/api/indexes.html#get-one-index):
`client.getRawIndex(uid: string): Promise<IndexResponse>`
`client.getRawIndex(uid: string): Promise<IndexObject>`

- [Get an object with information about the index](https://docs.meilisearch.com/reference/api/indexes.html#get-one-index):
`index.getRawInfo(): Promise<IndexResponse>`
`index.getRawInfo(): Promise<IndexObject>`

- [Update Index](https://docs.meilisearch.com/reference/api/indexes.html#update-an-index):

Expand Down Expand Up @@ -617,7 +617,7 @@ Using the index object:

- [Get keys](https://docs.meilisearch.com/reference/api/keys.html#get-all-keys):

`client.getKeys(): Promise<Result<Key[]>>`
`client.getKeys(parameters: KeysQuery): Promise<KeysResults>`

- [Get one key](https://docs.meilisearch.com/reference/api/keys.html#get-one-key):

Expand Down
51 changes: 34 additions & 17 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ import {
KeyCreation,
Config,
IndexOptions,
IndexResponse,
IndexObject,
EnqueuedTask,
Key,
Health,
Stats,
Version,
ErrorStatusCode,
Task,
Result,
TokenSearchRules,
TokenOptions,
TaskParams,
TasksQuery,
WaitOptions,
KeyUpdate,
IndexesQuery,
IndexesResults,
KeysQuery,
KeysResults,
TasksResults,
} from '../types'
import { HttpRequests } from '../http-requests'
import { TaskClient } from '../task'
Expand Down Expand Up @@ -76,21 +80,24 @@ class Client {
* @memberof MeiliSearch
* @method getRawIndex
* @param {string} indexUid The index UID
* @returns {Promise<IndexResponse>} Promise returning index information
* @returns {Promise<IndexObject>} Promise returning index information
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a kind of https://en.wikipedia.org/wiki/Hungarian_notation not sure if this is the best choice, why not just Index are you using it somewhere else??

Copy link
Contributor Author

@bidoubiwa bidoubiwa Jun 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Index is the instance of an Index class. While IndexObject is the object representation of that same information.

Index(...) vs { ... }

see here:

  async getIndexes(
    parameters: IndexesQuery = {}
  ): Promise<IndexesResults<Index[]>> {

*/
async getRawIndex(indexUid: string): Promise<IndexResponse> {
async getRawIndex(indexUid: string): Promise<IndexObject> {
return new Index(this.config, indexUid).getRawInfo()
}

/**
* Get all the indexes as Index instances.
* @memberof MeiliSearch
* @method getIndexes
* @param {IndexesQuery} [parameters={}] - Parameters to browse the indexes
*
* @returns {Promise<Result<Index[]>>} Promise returning array of raw index information
* @returns {Promise<IndexesResults<Index[]>>} Promise returning array of raw index information
*/
async getIndexes(): Promise<Result<Index[]>> {
const rawIndexes = await this.getRawIndexes()
async getIndexes(
parameters: IndexesQuery = {}
): Promise<IndexesResults<Index[]>> {
const rawIndexes = await this.getRawIndexes(parameters)
const indexes: Index[] = rawIndexes.results.map(
(index) => new Index(this.config, index.uid, index.primaryKey)
)
Expand All @@ -101,12 +108,18 @@ class Client {
* Get all the indexes in their raw value (no Index instances).
* @memberof MeiliSearch
* @method getRawIndexes
* @param {IndexesQuery} [parameters={}] - Parameters to browse the indexes
*
* @returns {Promise<Result<IndexResponse[]>>} Promise returning array of raw index information
* @returns {Promise<IndexesResults<IndexObject[]>>} Promise returning array of raw index information
*/
async getRawIndexes(): Promise<Result<IndexResponse[]>> {
async getRawIndexes(
parameters: IndexesQuery = {}
): Promise<IndexesResults<IndexObject[]>> {
const url = `indexes`
return await this.httpRequest.get<Result<IndexResponse[]>>(url)
return await this.httpRequest.get<IndexesResults<IndexObject[]>>(
url,
parameters
)
}

/**
Expand Down Expand Up @@ -179,10 +192,12 @@ class Client {
* Get the list of all client tasks
* @memberof MeiliSearch
* @method getTasks
* @returns {Promise<Result<Task[]>>} - Promise returning all tasks
* @param {TasksQuery} [parameters={}] - Parameters to browse the tasks
*
* @returns {Promise<TasksResults>} - Promise returning all tasks
*/
async getTasks(params?: TaskParams): Promise<Result<Task[]>> {
return await this.tasks.getTasks(params)
async getTasks(parameters: TasksQuery = {}): Promise<TasksResults> {
return await this.tasks.getTasks(parameters)
}

/**
Expand Down Expand Up @@ -245,11 +260,13 @@ class Client {
* Get all API keys
* @memberof MeiliSearch
* @method getKeys
* @returns {Promise<Keys>} Promise returning an object with keys
* @param {KeysQuery} [parameters={}] - Parameters to browse the indexes
*
* @returns {Promise<KeysResults>} Promise returning an object with keys
*/
async getKeys(): Promise<Result<Key[]>> {
async getKeys(parameters: KeysQuery = {}): Promise<KeysResults> {
const url = `keys`
return await this.httpRequest.get<Result<Key[]>>(url)
return await this.httpRequest.get<KeysResults>(url, parameters)
}

/**
Expand Down
58 changes: 31 additions & 27 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import {
SearchParams,
Filter,
SearchRequestGET,
IndexResponse,
IndexObject,
IndexOptions,
IndexStats,
DocumentsParams,
Documents,
DocumentsQuery,
Document,
AddDocumentParams,
DocumentOptions,
EnqueuedTask,
Settings,
Synonyms,
Expand All @@ -34,8 +33,10 @@ import {
SearchableAttributes,
DisplayedAttributes,
TypoTolerance,
Result,
WaitOptions,
DocumentsResults,
TasksQuery,
TasksResults,
} from './types'
import { removeUndefinedFromObject } from './utils'
import { HttpRequests } from './http-requests'
Expand Down Expand Up @@ -142,11 +143,12 @@ class Index<T = Record<string, any>> {
* Get index information.
* @memberof Index
* @method getRawInfo
* @returns {Promise<IndexResponse>} Promise containing index information
*
* @returns {Promise<IndexObject>} Promise containing index information
*/
async getRawInfo(): Promise<IndexResponse> {
async getRawInfo(): Promise<IndexObject> {
const url = `indexes/${this.uid}`
const res = await this.httpRequest.get<IndexResponse>(url)
const res = await this.httpRequest.get<IndexObject>(url)
this.primaryKey = res.primaryKey
this.updatedAt = new Date(res.updatedAt)
this.createdAt = new Date(res.createdAt)
Expand Down Expand Up @@ -227,11 +229,12 @@ class Index<T = Record<string, any>> {
*
* @memberof Indexes
* @method getTasks
* @param {TasksQuery} [parameters={}] - Parameters to browse the tasks
*
* @returns {Promise<Result<Task[]>>} - Promise containing all tasks
* @returns {Promise<TasksResults>} - Promise containing all tasks
*/
async getTasks(): Promise<Result<Task[]>> {
return await this.tasks.getTasks({ indexUid: [this.uid] })
async getTasks(parameters: TasksQuery = {}): Promise<TasksResults> {
return await this.tasks.getTasks({ ...parameters, indexUid: [this.uid] })
}

/**
Expand Down Expand Up @@ -310,25 +313,25 @@ class Index<T = Record<string, any>> {
* @memberof Index
* @method getDocuments
* @template T
* @param {DocumentsParams<T>} options? Options to browse the documents
* @returns {Promise<Result<Documents<T>>>} Promise containing Document responses
* @param {DocumentsQuery<T>} [parameters={}] Parameters to browse the documents
* @returns {Promise<DocumentsResults<T>>>} Promise containing Document responses
*/
async getDocuments<T = Record<string, any>>(
options?: DocumentsParams<T>
): Promise<Result<Documents<T>>> {
parameters: DocumentsQuery<T> = {}
): Promise<DocumentsResults<T>> {
const url = `indexes/${this.uid}/documents`

const fields = (() => {
if (Array.isArray(options?.fields)) {
return options?.fields?.join(',')
if (Array.isArray(parameters?.fields)) {
return parameters?.fields?.join(',')
}
return undefined
})()

return await this.httpRequest.get<Promise<Result<Documents<T>>>>(
return await this.httpRequest.get<Promise<DocumentsResults<T>>>(
url,
removeUndefinedFromObject({
...options,
...parameters,
fields,
})
)
Expand All @@ -353,12 +356,13 @@ class Index<T = Record<string, any>> {
* @method addDocuments
* @template T
* @param {Array<Document<T>>} documents Array of Document objects to add/replace
* @param {AddDocumentParams} options? Query parameters
* @param {DocumentOptions} options? Options on document addition
*
* @returns {Promise<EnqueuedTask>} Promise containing object of the enqueued task
*/
async addDocuments(
documents: Array<Document<T>>,
options?: AddDocumentParams
options?: DocumentOptions
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents`
return await this.httpRequest.post(url, documents, options)
Expand All @@ -371,13 +375,13 @@ class Index<T = Record<string, any>> {
* @template T
* @param {Array<Document<T>>} documents Array of Document objects to add/replace
* @param {number} batchSize Size of the batch
* @param {AddDocumentParams} options? Query parameters
* @param {DocumentOptions} options? Options on document addition
* @returns {Promise<EnqueuedTasks>} Promise containing array of enqueued task objects for each batch
*/
async addDocumentsInBatches(
documents: Array<Document<T>>,
batchSize = 1000,
options?: AddDocumentParams
options?: DocumentOptions
): Promise<EnqueuedTask[]> {
const updates = []
for (let i = 0; i < documents.length; i += batchSize) {
Expand All @@ -393,12 +397,12 @@ class Index<T = Record<string, any>> {
* @memberof Index
* @method updateDocuments
* @param {Array<Document<Partial<T>>>} documents Array of Document objects to add/update
* @param {AddDocumentParams} options? Query parameters
* @param {DocumentOptions} options? Options on document update
* @returns {Promise<EnqueuedTask>} Promise containing object of the enqueued task
*/
async updateDocuments(
documents: Array<Document<Partial<T>>>,
options?: AddDocumentParams
options?: DocumentOptions
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents`
return await this.httpRequest.put(url, documents, options)
Expand All @@ -411,13 +415,13 @@ class Index<T = Record<string, any>> {
* @template T
* @param {Array<Document<T>>} documents Array of Document objects to add/update
* @param {number} batchSize Size of the batch
* @param {AddDocumentParams} options? Query parameters
* @param {DocumentOptions} options? Options on document update
* @returns {Promise<EnqueuedTasks>} Promise containing array of enqueued task objects for each batch
*/
async updateDocumentsInBatches(
documents: Array<Document<Partial<T>>>,
batchSize = 1000,
options?: AddDocumentParams
options?: DocumentOptions
): Promise<EnqueuedTask[]> {
const updates = []
for (let i = 0; i < documents.length; i += batchSize) {
Expand Down
22 changes: 13 additions & 9 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
Task,
WaitOptions,
TaskStatus,
Result,
TaskParams,
TasksQuery,
TasksResults,
} from './types'
import { HttpRequests } from './http-requests'
import { removeUndefinedFromObject, sleep } from './utils'
Expand All @@ -28,22 +28,26 @@ class TaskClient {
const url = `tasks/${uid}`
return await this.httpRequest.get<Task>(url)
}

/**
* Get tasks
*
* @param {TaskParams} params - query parameters
* @param {TasksQuery} [parameters={}] - Parameters to browse the tasks
*
* @returns { Promise<Result<Task[]>> }
* @returns {Promise<TasksResults>} - Promise containing all tasks
*/
async getTasks(params: TaskParams = {}): Promise<Result<Task[]>> {
async getTasks(parameters: TasksQuery = {}): Promise<TasksResults> {
const url = `tasks`

const queryParams = {
indexUid: params?.indexUid?.join(','),
type: params?.type?.join(','),
status: params?.status?.join(','),
indexUid: parameters?.indexUid?.join(','),
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
type: parameters?.type?.join(','),
status: parameters?.status?.join(','),
from: parameters.from,
limit: parameters.limit,
}
return await this.httpRequest.get<Result<Task[]>>(

return await this.httpRequest.get<Promise<TasksResults>>(
url,
removeUndefinedFromObject(queryParams)
)
Expand Down
Loading