Skip to content

Commit

Permalink
feat: WIP new node SDK.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Dec 20, 2021
1 parent d428df5 commit 7b1fd7b
Show file tree
Hide file tree
Showing 16 changed files with 1,353 additions and 1,067 deletions.
6 changes: 6 additions & 0 deletions src/api/documents/v0/document-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export class DocumentRef<T extends { [key: string]: any }> {
request,
(error, response: DocumentGetResponse) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else if (response.hasDocument()) {
const document = response.getDocument();
Expand Down Expand Up @@ -86,6 +88,8 @@ export class DocumentRef<T extends { [key: string]: any }> {
return new Promise<void>((resolve, reject) => {
this.documentClient.set(request, (error) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve();
Expand All @@ -104,6 +108,8 @@ export class DocumentRef<T extends { [key: string]: any }> {
return new Promise<void>((resolve, reject) => {
this.documentClient.delete(request, (error) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve();
Expand Down
16 changes: 11 additions & 5 deletions src/api/documents/v0/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
DocumentQueryStreamRequest,
DocumentQueryStreamResponse,
} from '@nitric/api/proto/document/v1/document_pb';
import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_grpc_pb'
import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_grpc_pb';
import { WhereQueryOperator, WhereValueExpression } from '../../../types';
import type { Map as ProtobufMap } from 'google-protobuf';
import { DocumentRef } from './document-ref';
Expand Down Expand Up @@ -138,7 +138,9 @@ export class Query<T extends { [key: string]: any }> {
*/
public limit(limit: number): Query<T> {
if (typeof limit !== 'number' || limit < 0) {
throw new InvalidArgumentError('limit must be a positive integer or 0 for unlimited.');
throw new InvalidArgumentError(
'limit must be a positive integer or 0 for unlimited.'
);
}

this.fetchLimit = limit;
Expand All @@ -148,7 +150,7 @@ export class Query<T extends { [key: string]: any }> {
public async fetch() {
const request = new DocumentQueryRequest();

request.setCollection(this.collection["toWire"]());
request.setCollection(this.collection['toWire']());
request.setLimit(this.fetchLimit);

if (this.expressions.length) {
Expand All @@ -171,6 +173,8 @@ export class Query<T extends { [key: string]: any }> {
request,
(error, response: DocumentQueryResponse) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
const pagingTokenMap = protoMapToMap(response.getPagingTokenMap());
Expand Down Expand Up @@ -205,7 +209,7 @@ export class Query<T extends { [key: string]: any }> {
protected getStreamRequest() {
const request = new DocumentQueryStreamRequest();

request.setCollection(this.collection["toWire"]());
request.setCollection(this.collection['toWire']());
request.setLimit(this.fetchLimit);
request.setExpressionsList(this.expressions);

Expand Down Expand Up @@ -255,7 +259,9 @@ export class Query<T extends { [key: string]: any }> {
},
});

responseStream.on('error', (e) => transform.destroy(fromGrpcError(e as ServiceError)));
responseStream.on('error', (e) =>
transform.destroy(fromGrpcError(e as ServiceError))
);
responseStream.pipe(transform);

return transform;
Expand Down
14 changes: 12 additions & 2 deletions src/api/events/v0/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { SERVICE_BIND } from '../../../constants';
import { EventServiceClient, TopicServiceClient } from '@nitric/api/proto/event/v1/event_grpc_pb';
import { NitricEvent as PbEvent, EventPublishRequest } from '@nitric/api/proto/event/v1/event_pb';
import {
EventServiceClient,
TopicServiceClient,
} from '@nitric/api/proto/event/v1/event_grpc_pb';
import {
NitricEvent as PbEvent,
EventPublishRequest,
} from '@nitric/api/proto/event/v1/event_pb';
import { Struct } from 'google-protobuf/google/protobuf/struct_pb';
import * as grpc from '@grpc/grpc-js';
import type { NitricEvent } from '../../../types';
Expand Down Expand Up @@ -78,6 +84,8 @@ export class Topic {
return new Promise<NitricEvent>((resolve, reject) => {
this.eventing.EventServiceClient.publish(request, (error, response) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve({ ...event, id: response.getId() });
Expand Down Expand Up @@ -148,6 +156,8 @@ export class Eventing {
return new Promise((resolve, reject) => {
this.TopicServiceClient.list(null, (error, response) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve(
Expand Down
46 changes: 31 additions & 15 deletions src/api/queues/v0/queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { QueueServiceClient } from '@nitric/api/proto/queue/v1/queue_grpc_pb';
import {
NitricTask,
QueueSendRequest,
QueueSendBatchRequest,
import {
NitricTask,
QueueSendRequest,
QueueSendBatchRequest,
QueueReceiveRequest,
QueueCompleteRequest,
} from '@nitric/api/proto/queue/v1/queue_pb';
import { SERVICE_BIND } from '../../../constants';
import * as grpc from '@grpc/grpc-js';
import type { Task } from '../../../types';
import { Struct } from 'google-protobuf/google/protobuf/struct_pb';
import { fromGrpcError, InvalidArgumentError, InternalError } from '../../errors';
import {
fromGrpcError,
InvalidArgumentError,
InternalError,
} from '../../errors';

/**
* A message that has failed to be enqueued
Expand Down Expand Up @@ -105,17 +109,23 @@ export class Queue {
* };
* });
*/
async send(tasks: Task[]): Promise<FailedMessage[]>;
async send(tasks: Task): Promise<void>;
async send(tasks: Task | Task[]): Promise<void | FailedMessage[]> {
async send(tasks: Task[]): Promise<FailedMessage[]>;
async send(tasks: Task): Promise<void>;
async send(tasks: Task | Task[]): Promise<void | FailedMessage[]> {
return new Promise((resolve, reject) => {
const request = new QueueSendBatchRequest();

request.setTasksList(Array.isArray(tasks) ? tasks.map(task => taskToWire(task)) : [taskToWire(tasks)]);
request.setTasksList(
Array.isArray(tasks)
? tasks.map((task) => taskToWire(task))
: [taskToWire(tasks)]
);
request.setQueue(this.name);

this.queueing.QueueServiceClient.sendBatch(request, (error, response) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
}
const failedTasks = response.getFailedtasksList().map((m) => ({
Expand All @@ -125,17 +135,19 @@ export class Queue {
payloadType: m.getTask().getPayloadType(),
},
message: m.getMessage(),
}))
if (!Array.isArray(tasks)) { // Single Task returns
if (failedTasks.length > 0) {
}));
if (!Array.isArray(tasks)) {
// Single Task returns
if (failedTasks.length > 0) {
reject(new InternalError(failedTasks[0].message));
}
resolve();
} else { // Array of Tasks return
resolve(failedTasks)
} else {
// Array of Tasks return
resolve(failedTasks);
}
});
});
});
}
/**
* Pop 1 or more queue items from the specified queue up to the depth limit.
Expand Down Expand Up @@ -173,6 +185,8 @@ export class Queue {

this.queueing.QueueServiceClient.receive(request, (error, response) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve(
Expand Down Expand Up @@ -242,6 +256,8 @@ export class ReceivedTask implements Task {
return await new Promise((resolve, reject) => {
this.queue.queueing.QueueServiceClient.complete(request, (error) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve();
Expand Down
18 changes: 12 additions & 6 deletions src/api/secrets/v0/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.
import { SERVICE_BIND } from '../../../constants';
import { SecretServiceClient } from '@nitric/api/proto/secret/v1/secret_grpc_pb';
import {
SecretPutRequest,
SecretPutResponse,
SecretAccessRequest,
SecretAccessResponse,
import {
SecretPutRequest,
SecretPutResponse,
SecretAccessRequest,
SecretAccessResponse,
SecretVersion as GrpcSecretVersion,
Secret as GrpcSecret,
} from '@nitric/api/proto/secret/v1/secret_pb';
Expand Down Expand Up @@ -99,6 +99,8 @@ class Secret {
request,
(error, response: SecretPutResponse) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve(
Expand Down Expand Up @@ -148,7 +150,9 @@ class Secret {
*/
version = (version: string) => {
if (!version) {
throw new InvalidArgumentError('A version is required to create a version reference.');
throw new InvalidArgumentError(
'A version is required to create a version reference.'
);
}
return new SecretVersion(this.secrets, this, version);
};
Expand Down Expand Up @@ -200,6 +204,8 @@ class SecretVersion {
request,
(error, response: SecretAccessResponse) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
const secretVersion = new SecretVersion(
Expand Down
45 changes: 31 additions & 14 deletions src/api/storage/v0/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
// limitations under the License.
import { SERVICE_BIND } from '../../../constants';
import { StorageServiceClient } from '@nitric/api/proto/storage/v1/storage_grpc_pb';
import { StorageWriteRequest, StorageReadRequest, StorageDeleteRequest, StoragePreSignUrlRequest } from '@nitric/api/proto/storage/v1/storage_pb';
import {
StorageWriteRequest,
StorageReadRequest,
StorageDeleteRequest,
StoragePreSignUrlRequest,
} from '@nitric/api/proto/storage/v1/storage_pb';
import * as grpc from '@grpc/grpc-js';
import { fromGrpcError, InvalidArgumentError } from '../../errors';

Expand All @@ -32,7 +37,9 @@ export class Storage {

bucket = (name: string): Bucket => {
if (!name) {
throw new InvalidArgumentError('A bucket name is required to use a Bucket.');
throw new InvalidArgumentError(
'A bucket name is required to use a Bucket.'
);
}
return new Bucket(this, name);
};
Expand All @@ -52,15 +59,17 @@ class Bucket {

file = (name: string) => {
if (!name) {
throw new InvalidArgumentError('A file name/path is required to use a File.');
throw new InvalidArgumentError(
'A file name/path is required to use a File.'
);
}
return new File(this.storage, this, name);
};
}

export enum FileMode {
Read = 0,
Write = 1
Write = 1,
}

export interface SignUrlOptions {
Expand All @@ -69,7 +78,7 @@ export interface SignUrlOptions {

const DEFAULT_SIGN_URL_OPTS = {
expiry: 600,
}
};

/**
* A reference to a file in a bucket.
Expand All @@ -88,11 +97,14 @@ class File {
/**
* Create a presigned url for reading or writing for the given file reference
*/
signUrl = async (mode: FileMode, opts: SignUrlOptions = DEFAULT_SIGN_URL_OPTS): Promise<string> => {
signUrl = async (
mode: FileMode,
opts: SignUrlOptions = DEFAULT_SIGN_URL_OPTS
): Promise<string> => {
const { expiry } = {
// inject default options in case where some are undefined
...DEFAULT_SIGN_URL_OPTS,
...opts,
...opts,
};
const request = new StoragePreSignUrlRequest();
request.setBucketName(this.bucket.name);
Expand All @@ -101,15 +113,20 @@ class File {
request.setExpiry(expiry);

return new Promise((resolve, reject) => {
this.storage.StorageServiceClient.preSignUrl(request, (error, response) => {
if (error) {
reject(fromGrpcError(error));
} else {
resolve(response.getUrl());
this.storage.StorageServiceClient.preSignUrl(
request,
(error, response) => {
if (error) {
// TODO: remove this ignore when not using link
// @ts-ignore
reject(fromGrpcError(error));
} else {
resolve(response.getUrl());
}
}
});
);
});
}
};

/**
* Write a an array of bytes to this file
Expand Down
Loading

0 comments on commit 7b1fd7b

Please sign in to comment.