Skip to content

Commit

Permalink
use enum for bucket notification type
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur committed May 15, 2023
1 parent b08c027 commit ff9c003
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"repository": "https://github.com/nitrictech/node-sdk",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"version": "0.0.1",
"scripts": {
"bump": "standard-version",
"build": "tsup src/index.ts --dts --outDir lib",
Expand Down
35 changes: 24 additions & 11 deletions src/api/storage/v0/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { FileMode, Storage } from './storage';
import { Bucket, FileMode, Storage } from './storage';
import { StorageServiceClient as GrpcStorageClient } from '@nitric/api/proto/storage/v1/storage_grpc_pb';
import {
StorageWriteResponse,
Expand All @@ -24,6 +24,7 @@ import {
} from '@nitric/api/proto/storage/v1/storage_pb';
import { UnimplementedError } from '../../errors';
import {
BucketNotificationType,
BucketNotificationWorkerOptions,
FileNotificationWorkerOptions,
bucket,
Expand Down Expand Up @@ -449,7 +450,11 @@ describe('bucket notification', () => {
});

beforeAll(async () => {
await bucket('test-bucket').on('write', 'test.png', mockFn);
await bucket('test-bucket').on(
BucketNotificationType.Write,
'test.png',
mockFn
);
});

it('should create a new FaasClient', () => {
Expand All @@ -459,7 +464,7 @@ describe('bucket notification', () => {
it('should provide Faas with BucketNotificationWorkerOptions', () => {
const expectedOpts = new BucketNotificationWorkerOptions(
'test-bucket',
'write',
BucketNotificationType.Write,
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
Expand All @@ -476,7 +481,11 @@ describe('bucket notification', () => {
});

beforeAll(async () => {
await bucket('test-bucket').on('delete', 'test.png', mockFn);
await bucket('test-bucket').on(
BucketNotificationType.Delete,
'test.png',
mockFn
);
});

it('should create a new FaasClient', () => {
Expand All @@ -486,7 +495,7 @@ describe('bucket notification', () => {
it('should provide Faas with BucketNotificationWorkerOptions', () => {
const expectedOpts = new BucketNotificationWorkerOptions(
'test-bucket',
'delete',
BucketNotificationType.Delete,
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
Expand Down Expand Up @@ -514,10 +523,10 @@ describe('file notification', () => {
const mockFn = jest.fn();

describe('When registering a file notification for creating', () => {
let bucketResource;
let bucketResource: Bucket;
beforeAll(async () => {
bucketResource = bucket('test-bucket-create').for('reading');
await bucketResource.on('write', 'test.png', mockFn);
await bucketResource.on(BucketNotificationType.Write, 'test.png', mockFn);
});

afterAll(() => {
Expand All @@ -535,7 +544,7 @@ describe('file notification', () => {
it('should provide Faas with FileNotificationWorkerOptions', () => {
const expectedOpts = new FileNotificationWorkerOptions(
bucketResource,
'write',
BucketNotificationType.Write,
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
Expand All @@ -547,10 +556,14 @@ describe('file notification', () => {
});

describe('When registering a file notification for deleting', () => {
let bucketResource;
let bucketResource: Bucket;
beforeAll(async () => {
bucketResource = bucket('test-bucket-delete').for('reading');
await bucketResource.on('delete', 'test.png', mockFn);
await bucketResource.on(
BucketNotificationType.Delete,
'test.png',
mockFn
);
});

afterAll(() => {
Expand All @@ -568,7 +581,7 @@ describe('file notification', () => {
it('should provide Faas with FileNotificationWorkerOptions', () => {
const expectedOpts = new FileNotificationWorkerOptions(
bucketResource,
'delete',
BucketNotificationType.Delete,
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
Expand Down
8 changes: 6 additions & 2 deletions src/api/storage/v0/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {
BucketNotificationMiddleware,
FileNotificationMiddleware,
} from '@nitric/sdk/faas';
import { BucketNotification, FileNotification } from '@nitric/sdk/resources';
import {
BucketNotification,
BucketNotificationType,
FileNotification,
} from '@nitric/sdk/resources';

/**
* Nitric storage client, facilitates writing and reading from blob storage (buckets).
Expand Down Expand Up @@ -112,7 +116,7 @@ export class Bucket {
* @returns Promise which resolves when the handler server terminates
*/
on(
notificationType: string,
notificationType: BucketNotificationType,
notificationPrefixFilter: string,
...middleware: FileNotificationMiddleware[]
): Promise<void> {
Expand Down
27 changes: 16 additions & 11 deletions src/resources/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@ import {
BucketNotificationMiddleware,
FileNotificationMiddleware,
} from '../faas';
import { BucketNotificationType } from '../gen/proto/faas/v1/faas_pb';
import { BucketNotificationType as ProtoBucketNotificationType } from '../gen/proto/faas/v1/faas_pb';

type BucketPermission = 'reading' | 'writing' | 'deleting';

const everything: BucketPermission[] = ['reading', 'writing', 'deleting'];

export enum BucketNotificationType {
Write,
Delete,
}

export class BucketNotificationWorkerOptions {
public readonly bucket: string;
public readonly notificationType: 0 | 1 | 2;
public readonly notificationPrefixFilter: string;

constructor(
bucket: string,
notificationType: string,
notificationType: BucketNotificationType,
notificationPrefixFilter: string
) {
this.bucket = bucket;
Expand All @@ -50,12 +55,12 @@ export class BucketNotificationWorkerOptions {
this.notificationPrefixFilter = notificationPrefixFilter;
}

static toGrpcEvent(notificationType: string): 0 | 1 | 2 {
static toGrpcEvent(notificationType: BucketNotificationType): 0 | 1 | 2 {
switch (notificationType) {
case 'write':
return BucketNotificationType.CREATED;
case 'delete':
return BucketNotificationType.DELETED;
case BucketNotificationType.Write:
return ProtoBucketNotificationType.CREATED;
case BucketNotificationType.Delete:
return ProtoBucketNotificationType.DELETED;
default:
throw new Error(`notification type ${notificationType} is unsupported`);
}
Expand All @@ -67,7 +72,7 @@ export class FileNotificationWorkerOptions extends BucketNotificationWorkerOptio

constructor(
bucketRef: Bucket,
notificationType: string,
notificationType: BucketNotificationType,
notificationPrefixFilter: string
) {
super(bucketRef.name, notificationType, notificationPrefixFilter);
Expand All @@ -81,7 +86,7 @@ export class BucketNotification {

constructor(
bucket: string,
notificationType: string,
notificationType: BucketNotificationType,
notificationPrefixFilter,
...middleware: BucketNotificationMiddleware[]
) {
Expand All @@ -105,7 +110,7 @@ export class FileNotification {

constructor(
bucket: Bucket,
notificationType: string,
notificationType: BucketNotificationType,
notificationPrefixFilter,
...middleware: FileNotificationMiddleware[]
) {
Expand Down Expand Up @@ -161,7 +166,7 @@ export class BucketResource extends SecureResource<BucketPermission> {
* @returns Promise which resolves when the handler server terminates
*/
on(
notificationType: string,
notificationType: BucketNotificationType,
notificationPrefixFilter: string,
...middleware: BucketNotificationMiddleware[]
): Promise<void> {
Expand Down

0 comments on commit ff9c003

Please sign in to comment.