Skip to content

Commit

Permalink
feat: update collections list parameters (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpshr authored Feb 12, 2020
1 parent 09b64c8 commit dcb290a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 35 deletions.
11 changes: 10 additions & 1 deletion abstract-sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ interface Collections extends Endpoint {

list(
descriptor: ProjectDescriptor | BranchDescriptor,
options?: RequestOptions & { layersPerCollection?: number | "all" }
options?: CollectionsListOptions
): Promise<Collection[]>;

update(
Expand Down Expand Up @@ -420,6 +420,15 @@ type RawOptions = RequestOptions & {
filename?: string
};

type CollectionsListOptions = ListOptions & {
branchStatus?: string,
layersPerCollection?: number | "all",
search?: string,
sortBy?: string,
sortDir?: string,
userId?: string
};

type ReviewStatus = "REQUESTED" | "REJECTED" | "APPROVED";

type ActivityBase = {
Expand Down
33 changes: 32 additions & 1 deletion docs/abstract-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ A collection is a set of layers at the same or different commits on a branch, th

![CLI][cli-icon] ![API][api-icon]

`collections.list(ProjectDescriptor | BranchDescriptor, { ...RequestOptions, layersPerCollection?: number }): Promise<Collection[]>`
`collections.list(ProjectDescriptor | BranchDescriptor, CollectionsListOptions): Promise<Collection[]>`

List all collections for a branch

Expand All @@ -350,6 +350,22 @@ abstract.collections.list({
});
```

Search and sort specific collections

```js
abstract.collections.list({
projectId: "616daa90-1736-11e8-b8b0-8d1fec7aef78",
branchId: "master"
}, {
branchStatus: "active",
layersPerCollection: 5,
search: "search string",
sortBy: "updatedAt"
});
```
s
> Note: Collection searching and sorting is only available when using the [API transport](/docs/transports).
### Retrieve a collection

![CLI][cli-icon] ![API][api-icon]
Expand Down Expand Up @@ -1813,6 +1829,21 @@ Options objects that can be passed to different SDK endpoints.
}
```

### CollectionsListOptions
```js
{
branchStatus?: string,
layersPerCollection?: number | "all",
limit?: number,
offset?: number,
search?: string,
sortBy?: string,
sortDir?: string,
transportMode?: ("api" | "cli")[],
userId?: string
}
```

## Descriptors

Reference for the parameters required to load resources with the Abstract SDK.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abstract-sdk",
"version": "8.0.0-beta.9",
"version": "8.0.0-beta.11",
"description": "Universal JavaScript bindings for the Abstract API and CLI",
"keywords": [
"abstract",
Expand Down
40 changes: 32 additions & 8 deletions src/endpoints/Collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
BranchDescriptor,
Collection,
CollectionDescriptor,
CollectionsListOptions,
NewCollection,
ProjectDescriptor,
RequestOptions,
Expand All @@ -12,6 +13,11 @@ import type {
import Endpoint from "../endpoints/Endpoint";
import { wrap } from "../util/helpers";

// Version 16 returns cached thumbnails
const headers = {
"Abstract-Api-Version": "16"
};

export default class Collections extends Endpoint {
create(
descriptor: ProjectDescriptor,
Expand Down Expand Up @@ -68,23 +74,38 @@ export default class Collections extends Endpoint {

list(
descriptor: ProjectDescriptor | BranchDescriptor,
options: {
...RequestOptions,
layersPerCollection?: number | "all"
} = {}
options: CollectionsListOptions = {}
) {
const { layersPerCollection, ...requestOptions } = options;
const {
branchStatus,
layersPerCollection,
limit,
offset,
search,
sortBy,
sortDir,
userId,
...requestOptions
} = options;

return this.configureRequest<Promise<Collection[]>>({
api: async () => {
const { projectId, ...sanitizedDescriptor } = descriptor;
const query = querystring.stringify({
...sanitizedDescriptor,
layersPerCollection
branchStatus,
layersPerCollection,
limit,
offset,
search,
sortBy,
sortDir,
userId
});

const response = await this.apiRequest(
`projects/${projectId}/collections?${query}`
`projects/${projectId}/collections?${query}`,
{ headers }
);

return wrap(response.data.collections, response);
Expand All @@ -94,7 +115,10 @@ export default class Collections extends Endpoint {
const response = await this.cliRequest([
"collections",
descriptor.projectId,
...(descriptor.branchId ? ["--branch", descriptor.branchId] : [])
...(descriptor.branchId ? ["--branch", descriptor.branchId] : []),
...(layersPerCollection
? ["--layersLimit", String(layersPerCollection)]
: [])
]);

return wrap(response.data.collections, response);
Expand Down
10 changes: 10 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ export type RawProgressOptions = {
onProgress?: ProgressCallback
};

export type CollectionsListOptions = {
...ListOptions,
branchStatus?: string,
layersPerCollection?: number | "all",
search?: string,
sortBy?: string,
sortDir?: string,
userId?: string
};
export type AccessToken = ?string | ShareDescriptor;
export type AccessTokenOption =
| AccessToken // TODO: Deprecate?
Expand Down
78 changes: 54 additions & 24 deletions tests/endpoints/Collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,35 @@ describe("collections", () => {

describe("list", () => {
test("api", async () => {
mockAPI("/projects/project-id/collections?branchId=branch-id", {
data: {
collections: [
{
id: "collection-id"
}
]
mockAPI(
"/projects/project-id/collections?branchId=branch-id&branchStatus=active&layersPerCollection=1337&limit=10&offset=2&search=search&sortBy=sort-by&sortDir=sort-dir&userId=user-id",
{
data: {
collections: [
{
id: "collection-id"
}
]
}
}
});
);

const response = await API_CLIENT.collections.list({
projectId: "project-id",
branchId: "branch-id"
});
const response = await API_CLIENT.collections.list(
{
projectId: "project-id",
branchId: "branch-id"
},
{
branchStatus: "active",
layersPerCollection: 1337,
limit: 10,
offset: 2,
search: "search",
sortBy: "sort-by",
sortDir: "sort-dir",
userId: "user-id"
}
);

expect(response).toEqual([
{
Expand All @@ -105,20 +120,35 @@ describe("collections", () => {
});

test("cli", async () => {
mockCLI(["collections", "project-id", "--branch", "branch-id"], {
data: {
collections: [
{
id: "collection-id"
}
]
mockCLI(
[
"collections",
"project-id",
"--branch",
"branch-id",
"--layersLimit",
"1337"
],
{
data: {
collections: [
{
id: "collection-id"
}
]
}
}
});
);

const response = await CLI_CLIENT.collections.list({
projectId: "project-id",
branchId: "branch-id"
});
const response = await CLI_CLIENT.collections.list(
{
projectId: "project-id",
branchId: "branch-id"
},
{
layersPerCollection: 1337
}
);

expect(response).toEqual([
{
Expand Down

0 comments on commit dcb290a

Please sign in to comment.