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 collection upsert types #487

Merged
merged 2 commits into from
Dec 21, 2021
Merged
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
20 changes: 11 additions & 9 deletions src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ type BaseCollection<CollectionType> = {
id: string;
};

export type CollectionItem<CollectionType extends UR = UR> = CollectionType & {
id: string;
user_id?: string;
};

export type CollectionResponse<CollectionType extends UR = UR> = BaseCollection<CollectionType> & {
created_at: string;
foreign_id: string;
Expand All @@ -27,15 +32,12 @@ export type SelectCollectionAPIResponse<CollectionType extends UR = UR> = APIRes

export type UpsertCollectionAPIResponse<CollectionType extends UR = UR> = APIResponse & {
data: {
[key: string]: {
data: CollectionType;
ferhatelmas marked this conversation as resolved.
Show resolved Hide resolved
id: string;
}[];
[key: string]: CollectionItem<CollectionType>[];
};
};

export type UpsertManyCollectionRequest<CollectionType extends UR = UR> = {
[collection: string]: NewCollectionEntry<CollectionType>[];
[collection: string]: CollectionItem<CollectionType>[];
};

export class CollectionEntry<
Expand Down Expand Up @@ -269,7 +271,7 @@ export class Collections<
* @param {NewCollectionEntry<CollectionType> | NewCollectionEntry<CollectionType>[]} data - A single json object or an array of objects
* @return {Promise<UpsertCollectionAPIResponse<CollectionType>>}
*/
upsert(collection: string, data: NewCollectionEntry<CollectionType> | NewCollectionEntry<CollectionType>[]) {
upsert(collection: string, data: CollectionItem<CollectionType> | CollectionItem<CollectionType>[]) {
if (!this.client.usingApiSecret) {
throw new SiteError('This method can only be used server-side using your API Secret');
}
Expand All @@ -290,18 +292,18 @@ export class Collections<
* @method upsert
* @memberof Collections.prototype
* @param {string} collection collection name
* @param {UpsertManyCollectionRequest} data - A single json object that contains information of many collections
* @param {UpsertManyCollectionRequest} items - A single json object that contains information of many collections
* @return {Promise<UpsertCollectionAPIResponse<CollectionType>>}
*/
upsertMany(data: UpsertManyCollectionRequest) {
upsertMany(items: UpsertManyCollectionRequest) {
ferhatelmas marked this conversation as resolved.
Show resolved Hide resolved
if (!this.client.usingApiSecret) {
throw new SiteError('This method can only be used server-side using your API Secret');
}

return this.client.post<UpsertCollectionAPIResponse<CollectionType>>({
url: 'collections/',
serviceName: 'api',
body: { data },
body: { data: items },
token: this.client.getCollectionsToken(),
});
}
Expand Down