From 7c2897225fe5c3d2413e9c6ad085be67ba5ba3f4 Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Wed, 31 Jan 2024 11:27:43 +1100 Subject: [PATCH 1/2] chore: make linter error on all unused vars Also fixed all lint errors and warnings --- .eslintrc.js | 1 + src/api/documents/v0/collection-group-ref.ts | 1 - src/api/documents/v0/document-ref.ts | 2 +- src/api/documents/v0/documents.ts | 4 +++- src/api/documents/v0/query.ts | 8 ++++---- src/api/errors/cancelled.ts | 5 +---- src/api/events/v0/events.ts | 2 +- src/api/queues/v0/queues.ts | 7 +------ src/api/secrets/v0/secrets.ts | 2 +- src/api/storage/v0/storage.ts | 10 +++------- src/api/websocket/v0/websocket.ts | 2 +- src/faas/v0/context.ts | 11 ++++++----- src/faas/v0/json.ts | 2 +- src/faas/v0/start.ts | 1 - src/faas/v0/traceProvider.ts | 1 - src/resources/bucket.ts | 11 +++++++---- src/resources/collection.ts | 20 +++++++++++++++----- src/resources/http.ts | 2 +- src/resources/queue.ts | 8 +++++--- src/resources/schedule.ts | 2 +- src/resources/secret.ts | 6 ++++-- src/resources/topic.ts | 12 +++++++----- src/resources/websocket.ts | 5 +++-- 23 files changed, 67 insertions(+), 58 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index aba61082..97dba87a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -20,5 +20,6 @@ module.exports = { '@typescript-eslint/ban-ts-ignore': 'off', '@typescript-eslint/no-explicit-any': 'off', 'jsdoc/tag-lines': 'off', // not documented on jsdoc plugin site, unsure how to correct. + "@typescript-eslint/no-unused-vars": ["error", { "args": "all", "argsIgnorePattern": "^_" }], }, }; diff --git a/src/api/documents/v0/collection-group-ref.ts b/src/api/documents/v0/collection-group-ref.ts index e1396f49..af0b2e18 100644 --- a/src/api/documents/v0/collection-group-ref.ts +++ b/src/api/documents/v0/collection-group-ref.ts @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_grpc_pb'; -import { InvalidArgumentError } from '../../errors'; import { CollectionRef } from './collection-ref'; import { MAX_COLLECTION_DEPTH } from './constants'; import { DocumentRef, DocumentStructure } from './document-ref'; diff --git a/src/api/documents/v0/document-ref.ts b/src/api/documents/v0/document-ref.ts index 9b882933..41f7a520 100644 --- a/src/api/documents/v0/document-ref.ts +++ b/src/api/documents/v0/document-ref.ts @@ -20,7 +20,7 @@ import { DocumentDeleteRequest, } from '@nitric/api/proto/document/v1/document_pb'; import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_grpc_pb'; -import { fromGrpcError, InvalidArgumentError } from '../../errors'; +import { fromGrpcError } from '../../errors'; import { CollectionRef } from './collection-ref'; import { MAX_COLLECTION_DEPTH } from './constants'; diff --git a/src/api/documents/v0/documents.ts b/src/api/documents/v0/documents.ts index fc69c846..5f47d086 100644 --- a/src/api/documents/v0/documents.ts +++ b/src/api/documents/v0/documents.ts @@ -39,7 +39,9 @@ export class Documents { * @param name The name of the collection (required) * @returns The Collection instance */ - public collection(name: string) { + public collection( + name: string + ): CollectionRef { return new CollectionRef(this.documentClient, name); } } diff --git a/src/api/documents/v0/query.ts b/src/api/documents/v0/query.ts index 07faaddd..d5b7545b 100644 --- a/src/api/documents/v0/query.ts +++ b/src/api/documents/v0/query.ts @@ -26,7 +26,7 @@ import type { Map as ProtobufMap } from 'google-protobuf'; import { DocumentRef, DocumentStructure } from './document-ref'; import { CollectionRef } from './collection-ref'; import { DocumentSnapshot } from './document-snapshot'; -import { fromGrpcError, InvalidArgumentError } from '../../errors'; +import { fromGrpcError } from '../../errors'; import { ServiceError } from '@grpc/grpc-js'; type PagingToken = Map; @@ -146,7 +146,7 @@ export class Query { return this; } - public async fetch() { + public async fetch(): Promise> { const request = new DocumentQueryRequest(); request.setCollection(this.collection['toWire']()); @@ -203,7 +203,7 @@ export class Query { }); } - protected getStreamRequest() { + protected getStreamRequest(): DocumentQueryStreamRequest { const request = new DocumentQueryStreamRequest(); request.setCollection(this.collection['toWire']()); @@ -247,7 +247,7 @@ export class Query { const transform = new Transform({ objectMode: true, - transform: (result: DocumentQueryStreamResponse, encoding, callback) => { + transform: (result: DocumentQueryStreamResponse, _encoding, callback) => { const doc = result.getDocument(); callback( diff --git a/src/api/errors/cancelled.ts b/src/api/errors/cancelled.ts index 6b07245e..fdbac5dd 100644 --- a/src/api/errors/cancelled.ts +++ b/src/api/errors/cancelled.ts @@ -12,10 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { - ErrorDetails, - ErrorScope, -} from '@nitric/sdk/gen/proto/error/v1/error_pb'; +import { ErrorDetails } from '@nitric/sdk/gen/proto/error/v1/error_pb'; import { NitricPluginError } from './plugin-error'; /** diff --git a/src/api/events/v0/events.ts b/src/api/events/v0/events.ts index fbc3910a..7b827278 100644 --- a/src/api/events/v0/events.ts +++ b/src/api/events/v0/events.ts @@ -23,7 +23,7 @@ import { import { Struct } from 'google-protobuf/google/protobuf/struct_pb'; import * as grpc from '@grpc/grpc-js'; import { NitricEvent } from '../../../types'; -import { fromGrpcError, InvalidArgumentError } from '../../errors'; +import { fromGrpcError } from '../../errors'; /** * Construct event and topic service clients. diff --git a/src/api/queues/v0/queues.ts b/src/api/queues/v0/queues.ts index f1ee45eb..b1f4eacc 100644 --- a/src/api/queues/v0/queues.ts +++ b/src/api/queues/v0/queues.ts @@ -14,7 +14,6 @@ import { QueueServiceClient } from '@nitric/api/proto/queue/v1/queue_grpc_pb'; import { NitricTask as NitricTaskPb, - QueueSendRequest, QueueSendBatchRequest, QueueReceiveRequest, QueueCompleteRequest, @@ -23,11 +22,7 @@ import { SERVICE_BIND } from '../../../constants'; import * as grpc from '@grpc/grpc-js'; import { NitricTask } from '../../../types'; import { Struct } from 'google-protobuf/google/protobuf/struct_pb'; -import { - fromGrpcError, - InvalidArgumentError, - InternalError, -} from '../../errors'; +import { fromGrpcError } from '../../errors'; /** * A message that has failed to be enqueued diff --git a/src/api/secrets/v0/secrets.ts b/src/api/secrets/v0/secrets.ts index a6be5d25..b1e5a193 100644 --- a/src/api/secrets/v0/secrets.ts +++ b/src/api/secrets/v0/secrets.ts @@ -22,7 +22,7 @@ import { Secret as GrpcSecret, } from '@nitric/api/proto/secret/v1/secret_pb'; import * as grpc from '@grpc/grpc-js'; -import { fromGrpcError, InvalidArgumentError } from '../../errors'; +import { fromGrpcError } from '../../errors'; const ENCODER = new TextEncoder(); const DECODER = new TextDecoder(); diff --git a/src/api/storage/v0/storage.ts b/src/api/storage/v0/storage.ts index 19f70543..78188d4f 100644 --- a/src/api/storage/v0/storage.ts +++ b/src/api/storage/v0/storage.ts @@ -22,13 +22,9 @@ import { StorageExistsRequest, } from '@nitric/api/proto/storage/v1/storage_pb'; import * as grpc from '@grpc/grpc-js'; -import { fromGrpcError, InvalidArgumentError } from '../../errors'; +import { fromGrpcError } from '../../errors'; +import { FileNotificationMiddleware } from '@nitric/sdk/faas'; import { - BucketNotificationMiddleware, - FileNotificationMiddleware, -} from '@nitric/sdk/faas'; -import { - BucketNotification, BucketNotificationType, FileNotification, } from '@nitric/sdk/resources'; @@ -97,7 +93,7 @@ export class Bucket { }); } - public file(name: string) { + public file(name: string): File { if (!name) { throw new Error('A file name/path is required to use a File.'); } diff --git a/src/api/websocket/v0/websocket.ts b/src/api/websocket/v0/websocket.ts index 2e68f25e..2fc2d92a 100644 --- a/src/api/websocket/v0/websocket.ts +++ b/src/api/websocket/v0/websocket.ts @@ -56,7 +56,7 @@ export class Websocket { sendRequest.setData(payload); return new Promise((res, rej) => { - this.client.send(sendRequest, (error, data) => { + this.client.send(sendRequest, (error, _data) => { if (error) { rej(fromGrpcError(error)); } diff --git a/src/faas/v0/context.ts b/src/faas/v0/context.ts index ab3019a6..b361aca2 100644 --- a/src/faas/v0/context.ts +++ b/src/faas/v0/context.ts @@ -31,7 +31,6 @@ import { BucketNotificationWorkerOptions, FileNotificationWorkerOptions, SubscriptionWorkerOptions, - bucket, } from '@nitric/sdk'; import { FaasWorkerOptions } from './start'; @@ -244,7 +243,9 @@ export class HttpResponse { * * @returns HttpContext with body property set with an encoded JSON string and json headers set. */ - get json() { + get json(): ( + data: string | number | boolean | Record + ) => HttpContext { return jsonResponse(this.ctx); } } @@ -288,7 +289,7 @@ export class HttpContext extends TriggerContext { static fromGrpcTriggerRequest( trigger: TriggerRequest, - options?: ApiWorkerOptions + _options?: ApiWorkerOptions ): HttpContext { const http = trigger.getHttp(); const ctx = new HttpContext(); @@ -443,7 +444,7 @@ export class EventContext extends TriggerContext< static fromGrpcTriggerRequest( trigger: TriggerRequest, - options?: SubscriptionWorkerOptions + _options?: SubscriptionWorkerOptions ): EventContext { const topic = trigger.getTopic(); const ctx = new EventContext(); @@ -482,7 +483,7 @@ export class BucketNotificationContext extends TriggerContext< static fromGrpcTriggerRequest( trigger: TriggerRequest, - options?: BucketNotificationWorkerOptions + _options?: BucketNotificationWorkerOptions ): BucketNotificationContext { const ctx = new BucketNotificationContext(); const bucketConfig = trigger.getNotification().getBucket(); diff --git a/src/faas/v0/json.ts b/src/faas/v0/json.ts index b71719a4..a06c50ef 100644 --- a/src/faas/v0/json.ts +++ b/src/faas/v0/json.ts @@ -38,7 +38,7 @@ export const json = (): HttpMiddleware => (ctx: HttpContext, next) => { */ export const jsonResponse = (ctx: HttpContext) => - (data: string | number | boolean | Record) => { + (data: string | number | boolean | Record): HttpContext => { ctx.res.body = new TextEncoder().encode(JSON.stringify(data)); ctx.res.headers['Content-Type'] = ['application/json']; diff --git a/src/faas/v0/start.ts b/src/faas/v0/start.ts index 28a3f75b..59cda11c 100644 --- a/src/faas/v0/start.ts +++ b/src/faas/v0/start.ts @@ -35,7 +35,6 @@ import { HttpWorker, WebsocketResponseContext, WebsocketWorker, - WebsocketEvent, } from '@nitric/api/proto/faas/v1/faas_pb'; import { diff --git a/src/faas/v0/traceProvider.ts b/src/faas/v0/traceProvider.ts index b97eff35..e385137d 100644 --- a/src/faas/v0/traceProvider.ts +++ b/src/faas/v0/traceProvider.ts @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. import { - ConsoleSpanExporter, BatchSpanProcessor, NodeTracerProvider, NoopSpanProcessor, diff --git a/src/resources/bucket.ts b/src/resources/bucket.ts index 3179445f..a73af79c 100644 --- a/src/resources/bucket.ts +++ b/src/resources/bucket.ts @@ -18,6 +18,7 @@ import { ResourceDeclareResponse, ResourceDetailsResponse, ResourceType, + ResourceTypeMap, } from '@nitric/api/proto/resource/v1/resource_pb'; import resourceClient from './client'; import { storage, Bucket } from '../api/storage'; @@ -84,7 +85,7 @@ export class BucketNotification { constructor( bucket: string, notificationType: BucketNotificationType, - notificationPrefixFilter, + notificationPrefixFilter: string, ...middleware: BucketNotificationMiddleware[] ) { this.faas = new Faas( @@ -108,7 +109,7 @@ export class FileNotification { constructor( bucket: Bucket, notificationType: BucketNotificationType, - notificationPrefixFilter, + notificationPrefixFilter: string, ...middleware: FileNotificationMiddleware[] ) { this.faas = new Faas( @@ -195,10 +196,11 @@ export class BucketResource extends SecureResource { }, []); } - protected resourceType() { + protected resourceType(): ResourceTypeMap[keyof ResourceTypeMap] { return ResourceType.BUCKET; } + // eslint-disable-next-line @typescript-eslint/no-unused-vars protected unwrapDetails(resp: ResourceDetailsResponse): never { throw new Error('details unimplemented for bucket'); } @@ -208,7 +210,8 @@ export class BucketResource extends SecureResource { * * e.g. const imgs = resources.bucket('image').for('writing') * - * @param perms the required permission set + * @param perm the access that the currently scoped function is requesting to this resource. + * @param perms additional access that the currently scoped function is requesting to this resource. * @returns a usable bucket reference */ public for(perm: BucketPermission, ...perms: BucketPermission[]): Bucket { diff --git a/src/resources/collection.ts b/src/resources/collection.ts index e00838da..945a6f67 100644 --- a/src/resources/collection.ts +++ b/src/resources/collection.ts @@ -18,12 +18,15 @@ import { ResourceType, Action, ResourceDetailsResponse, + ResourceTypeMap, + ActionMap, } from '@nitric/api/proto/resource/v1/resource_pb'; import { fromGrpcError } from '../api/errors'; import { documents } from '../api/documents'; import resourceClient from './client'; import { make, SecureResource } from './common'; import { DocumentStructure } from '../api/documents/v0/document-ref'; +import { CollectionRef } from '../api/documents/v0/collection-ref'; type CollectionPermission = 'reading' | 'writing' | 'deleting'; @@ -51,7 +54,7 @@ export class CollectionResource< return new Promise((resolve, reject) => { resourceClient.declare( req, - (error, response: ResourceDeclareResponse) => { + (error, _response: ResourceDeclareResponse) => { if (error) { reject(fromGrpcError(error)); } else { @@ -62,7 +65,9 @@ export class CollectionResource< }); } - protected permsToActions(...perms: CollectionPermission[]) { + protected permsToActions( + ...perms: CollectionPermission[] + ): ActionMap[keyof ActionMap][] { let actions = perms.reduce((actions, perm) => { switch (perm) { case 'reading': @@ -91,10 +96,11 @@ export class CollectionResource< return actions; } - protected resourceType() { + protected resourceType(): ResourceTypeMap[keyof ResourceTypeMap] { return ResourceType.COLLECTION; } + // eslint-disable-next-line @typescript-eslint/no-unused-vars protected unwrapDetails(resp: ResourceDetailsResponse): never { throw new Error('details unimplemented for collection'); } @@ -104,10 +110,14 @@ export class CollectionResource< * * e.g. const customers = resources.collection('customers').for('reading', 'writing') * - * @param perms the required permission set + * @param perm the access that the currently scoped function is requesting to this resource. + * @param perms additional access that the currently scoped function is requesting to this resource. * @returns a usable collection reference */ - public for(perm: CollectionPermission, ...perms: CollectionPermission[]) { + public for( + perm: CollectionPermission, + ...perms: CollectionPermission[] + ): CollectionRef { this.registerPolicy(perm, ...perms); return documents().collection(this.name); diff --git a/src/resources/http.ts b/src/resources/http.ts index 775f3a5a..915fc8d2 100644 --- a/src/resources/http.ts +++ b/src/resources/http.ts @@ -57,7 +57,7 @@ class HttpWorker { export const http = ( app: NodeApplication | ListenerFunction, callback?: () => void -) => { +): void => { const unknownApp = app as any; const nodeApp = diff --git a/src/resources/queue.ts b/src/resources/queue.ts index ea581e6f..6608c0bc 100644 --- a/src/resources/queue.ts +++ b/src/resources/queue.ts @@ -17,6 +17,7 @@ import { ResourceType, Action, ResourceDeclareResponse, + ResourceTypeMap, } from '@nitric/api/proto/resource/v1/resource_pb'; import resourceClient from './client'; import { queues, Queue } from '../api/'; @@ -76,11 +77,11 @@ export class QueueResource< return actions; } - protected resourceType() { + protected resourceType(): ResourceTypeMap[keyof ResourceTypeMap] { return ResourceType.QUEUE; } - protected unwrapDetails(resp: ResourceDeclareResponse): never { + protected unwrapDetails(_resp: ResourceDeclareResponse): never { throw new Error('details unimplemented for queue'); } @@ -89,7 +90,8 @@ export class QueueResource< * * e.g. const taskQueue = resources.queue('work').for('sending') * - * @param perms the access that the currently scoped function is requesting to this resource. + * @param perm the access that the currently scoped function is requesting to this resource. + * @param perms additional access that the currently scoped function is requesting to this resource. * @returns a useable queue. */ public for(perm: QueuePermission, ...perms: QueuePermission[]): Queue { diff --git a/src/resources/schedule.ts b/src/resources/schedule.ts index 495f48ce..7b6d509f 100644 --- a/src/resources/schedule.ts +++ b/src/resources/schedule.ts @@ -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 { EventMiddleware, Faas, ScheduleMiddleware } from '../faas'; +import { Faas, ScheduleMiddleware } from '../faas'; const Frequencies = ['days', 'hours', 'minutes'] as const; diff --git a/src/resources/secret.ts b/src/resources/secret.ts index 7d9393d6..0a38fef6 100644 --- a/src/resources/secret.ts +++ b/src/resources/secret.ts @@ -18,6 +18,7 @@ import { ResourceDeclareRequest, ResourceDeclareResponse, ResourceDetailsResponse, + ResourceTypeMap, } from '@nitric/api/proto/resource/v1/resource_pb'; import resourceClient from './client'; import { secrets, Secret } from '../api/secrets'; @@ -43,7 +44,7 @@ export class SecretResource extends SecureResource { return new Promise((resolve, reject) => { resourceClient.declare( req, - (error, response: ResourceDeclareResponse) => { + (error, _response: ResourceDeclareResponse) => { if (error) { reject(fromGrpcError(error)); } else { @@ -71,10 +72,11 @@ export class SecretResource extends SecureResource { }, []); } - protected resourceType() { + protected resourceType(): ResourceTypeMap[keyof ResourceTypeMap] { return ResourceType.SECRET; } + // eslint-disable-next-line @typescript-eslint/no-unused-vars protected unwrapDetails(resp: ResourceDetailsResponse): never { throw new Error('details unimplemented for secret'); } diff --git a/src/resources/topic.ts b/src/resources/topic.ts index 965c82aa..c91e2a96 100644 --- a/src/resources/topic.ts +++ b/src/resources/topic.ts @@ -20,10 +20,10 @@ import { ResourceDeclareResponse, ResourceType, Action, + ResourceTypeMap, } from '@nitric/api/proto/resource/v1/resource_pb'; import { ActionsList, make, SecureResource } from './common'; import { fromGrpcError } from '../api/errors'; -import { NitricEvent } from '../types'; type TopicPermission = 'publishing'; @@ -72,7 +72,7 @@ export class TopicResource< return new Promise((resolve, reject) => { resourceClient.declare( req, - (error, response: ResourceDeclareResponse) => { + (error, _response: ResourceDeclareResponse) => { if (error) { reject(fromGrpcError(error)); } else { @@ -113,7 +113,7 @@ export class TopicResource< return sub['start'](); } - protected resourceType() { + protected resourceType(): ResourceTypeMap[keyof ResourceTypeMap] { return ResourceType.TOPIC; } @@ -122,8 +122,9 @@ export class TopicResource< * * Not used for topics. * - * @param resp {never} + * @param resp {object} */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars protected unwrapDetails(resp: ResourceDeclareResponse): never { throw new Error('details unimplemented for topic'); } @@ -133,7 +134,8 @@ export class TopicResource< * * e.g. const updates = resources.topic('updates').for('publishing') * - * @param perms the required permission set + * @param perm the access that the currently scoped function is requesting to this resource. + * @param perms additional access that the currently scoped function is requesting to this resource. * @returns a usable topic reference */ public for(perm: TopicPermission, ...perms: TopicPermission[]): Topic { diff --git a/src/resources/websocket.ts b/src/resources/websocket.ts index 76113f56..90223971 100644 --- a/src/resources/websocket.ts +++ b/src/resources/websocket.ts @@ -25,6 +25,7 @@ import { ResourceDeclareRequest, ResourceDetailsResponse, ResourceType, + ResourceTypeMap, } from '../gen/proto/resource/v1/resource_pb'; import resourceClient from './client'; import { make, Resource as Base } from './common'; @@ -164,11 +165,11 @@ export class WebsocketResource extends Base { return notification['start'](); } - protected resourceType() { + protected resourceType(): ResourceTypeMap[keyof ResourceTypeMap] { return ResourceType.WEBSOCKET; } - protected unwrapDetails(resp: ResourceDetailsResponse) { + protected unwrapDetails(resp: ResourceDetailsResponse): { url: string } { if (resp.hasWebsocket()) { return { url: resp.getWebsocket().getUrl(), From 5d0dbf0c3ae4acb55eb36f0bf06239a7b1b06e38 Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Wed, 31 Jan 2024 11:28:21 +1100 Subject: [PATCH 2/2] Add linting to CI/CD --- .github/workflows/test.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0f2715bd..d72557f4 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -30,6 +30,8 @@ jobs: git diff --cached --quiet - name: Check Formatting run: yarn prettier:check + - name: Linting + run: yarn lint - name: Build run: yarn build - name: Run tests