Skip to content

Commit

Permalink
Release v0.12.0 (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed May 22, 2023
2 parents 58998ca + 47639ed commit 28d29b0
Show file tree
Hide file tree
Showing 23 changed files with 8,762 additions and 893 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
</p>

<p align="center">
Build <a href="https://nitric.io">nitric</a> applications with Node.js
Build <a href="https://nitric.io">Nitric</a> applications with Node.js
</p>

<p align="center">
<img alt="Tests" src="https://github.com/nitrictech/node-sdk/actions/workflows/test.yaml/badge.svg?branch=main">
<a href="https://codecov.io/gh/nitrictech/node-sdk">
<img alt="codecov" src="https://codecov.io/gh/nitrictech/node-sdk/branch/main/graph/badge.svg?token=N46TTGPE4G">
<img alt="codecov" src="https://img.shields.io/codecov/c/github/nitrictech/node-sdk?style=for-the-badge">
</a>
<a href="https://npmjs.org/package/@nitric/sdk">
<img alt="Version" src="https://img.shields.io/npm/v/@nitric/sdk.svg">
<img alt="Version" src="https://img.shields.io/npm/v/@nitric/sdk.svg?style=for-the-badge">
</a>
<a href="https://npmjs.org/package/@nitric/sdk">
<img alt="Downloads/week" src="https://img.shields.io/npm/dw/@nitric/sdk.svg">
<img alt="Downloads/week" src="https://img.shields.io/npm/dw/@nitric/sdk.svg?style=for-the-badge">
</a>
<a href="https://discord.gg/Webemece5C"><img alt="Discord" src="https://img.shields.io/discord/955259353043173427?label=discord"></a>
<a href="https://discord.gg/Webemece5C"><img alt="Discord" src="https://img.shields.io/discord/955259353043173427?label=discord&style=for-the-badge"></a>
</p>

The NodeJS SDK supports the use of the Nitric framework with NodeJS 12+. For more information, check out the main [Nitric repo](https://github.com/nitrictech/nitric).
Expand Down
19 changes: 8 additions & 11 deletions assets/nitric-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nitric/sdk",
"description": "Nitric NodeJS client sdk",
"nitric": "v0.22.0",
"nitric": "v0.27.0",
"author": "Nitric <https://github.com/nitrictech>",
"repository": "https://github.com/nitrictech/node-sdk",
"main": "lib/index.js",
Expand Down
160 changes: 159 additions & 1 deletion 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 @@ -23,6 +23,15 @@ import {
File,
} from '@nitric/api/proto/storage/v1/storage_pb';
import { UnimplementedError } from '../../errors';
import {
BucketNotificationType,
BucketNotificationWorkerOptions,
FileNotificationWorkerOptions,
bucket,
} from '@nitric/sdk/resources';
import { faas } from '@nitric/sdk';
import { ResourceServiceClient } from '@nitric/sdk/gen/proto/resource/v1/resource_grpc_pb';
import { ResourceDeclareResponse } from '@nitric/sdk/gen/proto/resource/v1/resource_pb';

describe('Storage Client Tests', () => {
describe('Given nitric.api.storage.StorageClient.Write throws an error', () => {
Expand Down Expand Up @@ -422,3 +431,152 @@ describe('Storage Client Tests', () => {
});
});
});

jest.mock('../../../faas/index');

describe('bucket notification', () => {
const startSpy = jest
.spyOn(faas.Faas.prototype, 'start')
.mockReturnValue(Promise.resolve());
const mockFn = jest.fn();

afterAll(() => {
jest.clearAllMocks();
});

describe('When registering a bucket notification for creating', () => {
afterAll(() => {
jest.resetAllMocks();
});

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

it('should create a new FaasClient', () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it('should provide Faas with BucketNotificationWorkerOptions', () => {
const expectedOpts = new BucketNotificationWorkerOptions(
'test-bucket',
'write',
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});

it('should call FaasClient::start()', () => {
expect(startSpy).toBeCalledTimes(1);
});
});

describe('When registering a bucket notification for deleting', () => {
afterAll(() => {
jest.resetAllMocks();
});

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

it('should create a new FaasClient', () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it('should provide Faas with BucketNotificationWorkerOptions', () => {
const expectedOpts = new BucketNotificationWorkerOptions(
'test-bucket',
'delete',
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});

it('should call FaasClient::start()', () => {
expect(startSpy).toBeCalledTimes(1);
});
});
});

describe('file notification', () => {
const startSpy = jest
.spyOn(faas.Faas.prototype, 'start')
.mockReturnValue(Promise.resolve());

const existsSpy = jest
.spyOn(ResourceServiceClient.prototype, 'declare')
.mockImplementation((_, callback: any) => {
const response = new ResourceDeclareResponse();
callback(null, response);
return null as any;
});

const mockFn = jest.fn();

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

afterAll(() => {
jest.resetAllMocks();
});

it('should declare the new resource', () => {
expect(existsSpy).toBeCalledTimes(1);
});

it('should create a new FaasClient', () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it('should provide Faas with FileNotificationWorkerOptions', () => {
const expectedOpts = new FileNotificationWorkerOptions(
bucketResource,
'write',
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});

it('should call FaasClient::start()', () => {
expect(startSpy).toBeCalledTimes(1);
});
});

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

afterAll(() => {
jest.resetAllMocks();
});

it('should declare the new resource', () => {
expect(existsSpy).toBeCalledTimes(1);
});

it('should create a new FaasClient', () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it('should provide Faas with FileNotificationWorkerOptions', () => {
const expectedOpts = new FileNotificationWorkerOptions(
bucketResource,
'delete',
'test.png'
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});

it('should call FaasClient::start()', () => {
expect(startSpy).toBeCalledTimes(1);
});
});
});
33 changes: 32 additions & 1 deletion src/api/storage/v0/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import {
} from '@nitric/api/proto/storage/v1/storage_pb';
import * as grpc from '@grpc/grpc-js';
import { fromGrpcError, InvalidArgumentError } from '../../errors';
import {
BucketNotificationMiddleware,
FileNotificationMiddleware,
} from '@nitric/sdk/faas';
import {
BucketNotification,
BucketNotificationType,
FileNotification,
} from '@nitric/sdk/resources';

/**
* Nitric storage client, facilitates writing and reading from blob storage (buckets).
Expand Down Expand Up @@ -56,7 +65,7 @@ export class Storage {
* A reference to a storage bucket.
*/
export class Bucket {
storage: Storage;
private storage: Storage;
name: string;

constructor(storage: Storage, name: string) {
Expand Down Expand Up @@ -97,6 +106,28 @@ export class Bucket {
}
return new File(this.storage, this, name);
}

/**
* Register and start a bucket notification handler that will be called for all matching notification events on this bucket
*
* @param notificationType the notification type that should trigger the middleware, either 'write' or 'delete'
* @param notificationPrefixFilter the file name prefix that files must match to trigger a notification
* @param middleware handler middleware which will be run for every incoming event
* @returns Promise which resolves when the handler server terminates
*/
on(
notificationType: BucketNotificationType,
notificationPrefixFilter: string,
...middleware: FileNotificationMiddleware[]
): Promise<void> {
const notification = new FileNotification(
this,
notificationType,
notificationPrefixFilter,
...middleware
);
return notification['start']();
}
}

export enum FileMode {
Expand Down
6 changes: 5 additions & 1 deletion src/faas/v0/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
QueryValue,
TriggerResponse,
} from '@nitric/api/proto/faas/v1/faas_pb';
import { TriggerContext, HttpContext, EventContext } from './context';
import { TriggerContext, HttpContext, EventContext } from '.';

describe('NitricTrigger.fromGrpcTriggerRequest', () => {
describe('From a HttpTriggerRequest', () => {
Expand All @@ -39,6 +39,9 @@ describe('NitricTrigger.fromGrpcTriggerRequest', () => {
const testQuery = new QueryValue();
testQuery.addValue('test');
ctx.getQueryParamsMap().set('test', testQuery);
const testEncodedQuery = new QueryValue();
testEncodedQuery.addValue(encodeURIComponent('/path/here'));
ctx.getQueryParamsMap().set('test-encoded', testEncodedQuery);
const request = new TriggerRequest();
request.setData('Hello World');
request.setHttp(ctx);
Expand Down Expand Up @@ -69,6 +72,7 @@ describe('NitricTrigger.fromGrpcTriggerRequest', () => {

it('should have the provided query params', () => {
expect(trigger.http.req.query['test']).toBe('test');
expect(trigger.http.req.query['test-encoded']).toBe('/path/here');
});

it('should allow json response', () => {
Expand Down
Loading

0 comments on commit 28d29b0

Please sign in to comment.