Skip to content

Commit

Permalink
flow types.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Feb 2, 2017
1 parent 554c017 commit ce27f00
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
10 changes: 9 additions & 1 deletion interfaces/external-modules/kinto-http.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ declare module "kinto-http" {
body: Object,
};

declare type PermissionEntry = {
resource_name: "bucket" | "group" | "collection" | "record",
id: string,
bucket_id: string,
collection_id?: string,
permissions: string[],
};

declare class KintoClient {
remote: string;
defaultReqOptions: {
Expand All @@ -45,7 +53,7 @@ declare module "kinto-http" {
batch(): Promise<BatchResponse[]>;
fetchServerInfo(): Promise<Object>;
listBuckets(): Promise<ListResponseBody<Resource>>;
listPermissions(): Promise<ListResponseBody<Object>>;
listPermissions(): Promise<ListResponseBody<PermissionEntry>>;
}

declare class Bucket {
Expand Down
5 changes: 3 additions & 2 deletions src/sagas/session.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */
import type { ActionType, GetStateFn, SagaGen } from "../types";
import type { PermissionEntry } from "kinto-http";
import type { ActionType, BucketEntry, GetStateFn, SagaGen } from "../types";

import { push as updatePath } from "react-router-redux";
import { call, put } from "redux-saga/effects";
Expand Down Expand Up @@ -34,7 +35,7 @@ export function* sessionLogout(getState: GetStateFn, action: ActionType<typeof a
yield call(clearSession);
}

export function expandBucketsCollections(buckets: Object[], permissions: Object[]) {
export function expandBucketsCollections(buckets: BucketEntry[], permissions: PermissionEntry[]): BucketEntry[] {
// Create a copy to avoid mutating the source object
const bucketsCopy = clone(buckets);

Expand Down
15 changes: 14 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,25 @@ export type TokenAuth = {

export type SagaGen = Generator<*,void,*>;

export type CollectionEntry = {
id: string,
permissions: string[],
readonly: boolean,
};

export type BucketEntry = {
id: string,
permissions: string[],
collections: CollectionEntry[],
readonly: boolean,
};

export type SessionState = {
busy: boolean,
auth: ?AuthData;
authenticated: boolean,
permissions: ?PermissionsListEntry[],
buckets: Object[],
buckets: BucketEntry[],
serverInfo: ServerInfo,
redirectURL: ?string,
};
Expand Down
14 changes: 12 additions & 2 deletions test/sagas/session_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ describe("expandBucketsCollections()", () => {
collection_id: "b2c1",
permissions: ["read"],
},
{
resource_name: "collection",
bucket_id: "b3",
collection_id: "b3c1",
permissions: ["read", "write"],
},
];

const tree = saga.expandBucketsCollections(buckets, permissions);
Expand All @@ -298,9 +304,13 @@ describe("expandBucketsCollections()", () => {
});

it("should denote a collection as readonly", () => {
const b1c1 = tree
const b2c1 = tree
.find(b => b.id === "b2").collections
.find(c => c.id === "b2c1");
expect(b1c1.readonly).to.be.true;
expect(b2c1.readonly).to.be.true;
});

it("should infer an implicit bucket", () => {
expect(tree.find(b => b.id === "b3").readonly).to.be.false;
});
});

0 comments on commit ce27f00

Please sign in to comment.