Skip to content

Commit

Permalink
feat: register buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Jan 4, 2022
1 parent 836c389 commit e508157
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/resources/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Resource, ResourceDeclareRequest, ResourceDeclareResponse, ResourceType } from "@nitric/api/proto/resource/v1/resource_pb";
import resourceClient from './client';
import { storage, Bucket, File } from "../api/storage";

type BucketPermission = "Read" | "Write" | "Delete";
Expand Down Expand Up @@ -29,6 +31,29 @@ class BucketResource {
this.name = name;
}

private async register(): Promise<void> {
const req = new ResourceDeclareRequest();
const resource = new Resource();
resource.setName(this.name);
resource.setType(ResourceType.BUCKET);
req.setResource(resource);

return new Promise<void>((resolve, reject) => {
resourceClient.declare(
req,
(error, response: ResourceDeclareResponse) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve();
}
}
);
});
}

/**
* Retrieve a BucketReference with all permissions
*/
Expand All @@ -46,12 +71,22 @@ class BucketResource {
}
}


// This singleton helps avoid duplicate references to bucket('name')
// will return the same bucket resource
const buckets: Record<string, BucketResource> = {};

/**
* Provides a cloud bucket resource.
*
* @param name the _unique_ name of the bucket within the stack
* @returns
*/
export const bucket = (name: string): BucketResource => {
return new BucketResource(name);
}
if (!buckets[name]) {
buckets[name] = new BucketResource(name);
buckets[name]['register']();
}

return buckets[name];
};
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './api';
export * from './topic';
export * from './queue';
export * from './collection';
export * from './bucket';
export * from './schedule';

0 comments on commit e508157

Please sign in to comment.