Skip to content

Commit

Permalink
feat: add batch collection layer support
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpshr committed Nov 13, 2019
1 parent ded4e91 commit 4f840b3
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
18 changes: 15 additions & 3 deletions abstract-sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class Client {
assets: Assets;
branches: Branches;
changesets: Changesets;
collectionLayers: CollectionLayers;
collections: Collections;
comments: Comments;
commits: Commits;
Expand Down Expand Up @@ -165,9 +166,10 @@ interface Changesets extends Endpoint {

interface CollectionLayers extends Endpoint {
add(descriptor: CollectionDescriptor, layer: NewCollectionLayer): Promise<CollectionLayer>;
addMany(descriptor: CollectionDescriptor, layers: NewCollectionLayer[]): Promise<CollectionLayer[]>;
remove(descriptor: CollectionLayerDescriptor): Promise<void>;
move(descriptor: CollectionLayerDescriptor, order: number): Promise<CollectionLayer[]>;
update(descriptor: CollectionLayerDescriptor, layer: NewCollectionLayer): Promise<CollectionLayer>;
update(descriptor: CollectionLayerDescriptor, layer: UpdatedCollectionLayer): Promise<CollectionLayer>;
}

interface Collections extends Endpoint {
Expand Down Expand Up @@ -854,14 +856,24 @@ type NewComment = {

type NewCollectionLayer = {
fileId: string,
isPinned: boolean,
isPinned?: boolean,
layerId: string,
order: number,
order?: number,
pageId: string,
sha: "latest" | string,
useLatestCommit?: boolean
};

type UpdatedCollectionLayer = {
fileId?: string,
isPinned?: boolean,
layerId?: string,
order?: number,
pageId?: string,
sha?: "latest" | string,
useLatestCommit?: boolean
};

type CollectionLayer = {
collectionId: string,
fileId: string,
Expand Down
30 changes: 27 additions & 3 deletions docs/abstract-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,13 @@ A collection layer represents an underlying layer within a collection. Collectio
| `order` | `number` | SHA of the commit that the underlying layer should point to |
| `sha` | `string` | SHA of the commit that the underlying layer should point to |

### Add a layer to a collection
### Add a single layer to a collection

![API][api-icon]

`collectionLayers.add(descriptor: CollectionDescriptor, layer: NewCollectionLayer): Promise<CollectionLayer>`

Add a layer to a collection
Add a single layer to a collection

```js
abstract.collectionLayers.add({
Expand All @@ -454,6 +454,30 @@ abstract.collectionLayers.add({
});
```

### Add multiple layers to a collection

![API][api-icon]

`collectionLayers.addMany(descriptor: CollectionDescriptor, layers: NewCollectionLayer[]): Promise<CollectionLayer[]>`

Add multiple layers to a collection

```js
abstract.collectionLayers.addMany({
projectId: '003a1ae0-a4b3-11e9-807c-a35b74e69da5',
collectionId: '2538be75-c38b-4008-8a60-cf2c0364727e'
}, [
{
fileId: '745EF992-C945-4B4C-BAFD-C6D45C45C6E2',
isPinned: true,
layerId: '9E2EB6C6-3681-4FCF-951E-50F7F0A0B0DE',
order: 1,
pageId: '7DC19A61-4D5F-4D75-BCAE-A589DF08257B',
sha: 'latest'
}
]);
```

### Remove a layer from a collection

![API][api-icon]
Expand All @@ -473,7 +497,7 @@ abstract.collectionLayers.remove({

![API][api-icon]

`collectionLayers.update(descriptor: CollectionLayerDescriptor, layer: NewCollectionLayer): Promise<CollectionLayer>`
`collectionLayers.update(descriptor: CollectionLayerDescriptor, layer: UpdatedCollectionLayer): Promise<CollectionLayer>`

Update a layer within a collection

Expand Down
27 changes: 25 additions & 2 deletions src/endpoints/CollectionLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type {
CollectionDescriptor,
CollectionLayer,
CollectionLayerDescriptor,
NewCollectionLayer
NewCollectionLayer,
UpdatedCollectionLayer
} from "../types";
import Endpoint from "./Endpoint";

Expand All @@ -24,6 +25,28 @@ export default class CollectionLayers extends Endpoint {
});
}

addMany(descriptor: CollectionDescriptor, layers: NewCollectionLayer[]) {
const collectionLayers = layers.map(layer => {
const { layerId, ...collectionLayer } = layer;
return { ...collectionLayer, id: layerId };
});
return this.request<Promise<CollectionLayer>>({
api: async () => {
const response = await this.apiRequest(
`projects/${descriptor.projectId}/collection_layers/create_many`,
{
method: "POST",
body: {
collectionId: descriptor.collectionId,
layers: collectionLayers
}
}
);
return response.data;
}
});
}

remove(descriptor: CollectionLayerDescriptor) {
return this.request<Promise<void>>({
api: async () => {
Expand Down Expand Up @@ -51,7 +74,7 @@ export default class CollectionLayers extends Endpoint {
});
}

update(descriptor: CollectionLayerDescriptor, layer: NewCollectionLayer) {
update(descriptor: CollectionLayerDescriptor, layer: UpdatedCollectionLayer) {
return this.request<Promise<CollectionLayer>>({
api: async () => {
const response = await this.apiRequest(
Expand Down
16 changes: 16 additions & 0 deletions src/endpoints/CollectionLayers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ describe("#add", () => {
});
});

describe("#addMany", () => {
test("api", async () => {
mockAPI(
"/projects/project-id/collection_layers/create_many",
{ data: [mockLayer] },
201,
"post"
);
const response = await API_CLIENT.collectionLayers.addMany(
{ projectId: "project-id", collectionId: "collection-id" },
[mockLayer]
);
expect(response).toEqual([mockLayer]);
});
});

describe("#remove", () => {
test("api", async () => {
mockAPI(
Expand Down
14 changes: 12 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,14 +569,24 @@ export type NewComment = {

export type NewCollectionLayer = {
fileId: string,
isPinned: boolean,
isPinned?: boolean,
layerId: string,
order: number,
order?: number,
pageId: string,
sha: "latest" | string,
useLatestCommit?: boolean
};

export type UpdatedCollectionLayer = {
fileId?: string,
isPinned?: boolean,
layerId?: string,
order?: number,
pageId?: string,
sha?: "latest" | string,
useLatestCommit?: boolean
};

export type CollectionLayer = {
collectionId: string,
fileId: string,
Expand Down

0 comments on commit 4f840b3

Please sign in to comment.