diff --git a/types/minio/index.d.ts b/types/minio/index.d.ts new file mode 100644 index 0000000000..e4f2a0613c --- /dev/null +++ b/types/minio/index.d.ts @@ -0,0 +1,230 @@ +// Type definitions for minio 4.0 +// Project: https://github.com/minio/minio-js#readme +// Definitions by: Barin Britva +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +// Import from dependencies +import { Stream } from 'stream'; +import EventEmitter = NodeJS.EventEmitter; + +// Exports only from typings +export type Region = 'us-east-1'|'us-west-1'|'us-west-2'|'eu-west-1'|'eu-central-1'|'ap-southeast-1'|'ap-northeast-1'|'ap-southeast-2'|'sa-east-1'|'cn-north-1'; +export type PolicyValue = 'none'|'readonly'|'writeonly'|'readwrite'; +export type NoResultCallback = (error: Error|null) => void; +export type ResultCallback = (error: Error|null, result: T) => void; + +export interface ClientOptions { + endPoint: string; + accessKey: string; + secretKey: string; + secure?: boolean; + port?: number; + region?: Region; +} + +export interface BucketItemFromList { + name: string; + creationDate: Date; +} + +export interface BucketItemCopy { + etag: string; + lastModified: Date; +} + +export interface BucketItem { + name: string; + prefix: string; + size: number; + etag: string; + lastModified: Date; +} + +export interface BucketItemStat { + size: number; + contentType: string; + etag: string; + // In documentation in this case string. Is it doc error? + lastModified: Date|string; +} + +export interface IncompleteUploadedBucketItem { + key: string; + uploadId: string; + size: number; +} + +export interface BucketStream extends Stream { + on(event: 'data', listener: (item: T) => void): this; +} + +export interface PostPolicyResult { + postURL: string; + formData: { + [key: string]: any; + }; +} + +// No need to export this. But without it - linter error. +export class TargetConfig { + setId(id: any): void; + addEvent(newEvent: any): void; + addFilterSuffix(suffix: any): void; + addFilterPrefix(prefix: any): void; +} + +// Exports from library +export class Client { + constructor(options: ClientOptions); + + // Bucket operations + makeBucket(bucketName: string, callback: NoResultCallback): void; + makeBucket(bucketName: string, region: Region, callback: NoResultCallback): void; + makeBucket(bucketName: string, region?: Region): Promise; + + listBuckets(callback: ResultCallback): void; + listBuckets(): Promise; + + // Doc contains error - no boolean value in result + bucketExists(bucketName: string, callback: NoResultCallback): void; + bucketExists(bucketName: string): Promise; + + removeBucket(bucketName: string, callback: NoResultCallback): void; + removeBucket(bucketName: string): Promise; + + listObjects(bucketName: string, prefix?: string, recursive?: boolean): BucketStream; + + listObjectsV2(bucketName: string, prefix?: string, recursive?: boolean): BucketStream; + + listIncompleteUploads(bucketName: string, prefix?: string, recursive?: boolean): BucketStream; + + // Object operations + getObject(bucketName: string, objectName: string, callback: ResultCallback): void; + getObject(bucketName: string, objectName: string): Promise; + + getPartialObject(bucketName: string, objectName: string, offset: number, callback: ResultCallback): void; + getPartialObject(bucketName: string, objectName: string, offset: number, length: number, callback: ResultCallback): void; + getPartialObject(bucketName: string, objectName: string, offset: number, length?: number): Promise; + + fGetObject(bucketName: string, objectName: string, filePath: string, callback: NoResultCallback): void; + fGetObject(bucketName: string, objectName: string, filePath: string): Promise; + + putObject(bucketName: string, objectName: string, stream: Stream|Buffer|string, callback: ResultCallback): void; + putObject(bucketName: string, objectName: string, stream: Stream|Buffer|string, size: number, callback: ResultCallback): void; + putObject(bucketName: string, objectName: string, stream: Stream|Buffer|string, size: number, cotentType: string, callback: ResultCallback): void; + putObject(bucketName: string, objectName: string, stream: Stream|Buffer|string, size?: number, cotentType?: string): Promise; + + fPutObject(bucketName: string, objectName: string, filePath: string, contentType: string, callback: ResultCallback): void; + fPutObject(bucketName: string, objectName: string, filePath: string, contentType: string): Promise; + + copyObject(bucketName: string, objectName: string, sourceObject: string, conditions: CopyConditions, callback: ResultCallback): void; + copyObject(bucketName: string, objectName: string, sourceObject: string, conditions: CopyConditions): Promise; + + statObject(bucketName: string, objectName: string, callback: ResultCallback): void; + statObject(bucketName: string, objectName: string): Promise; + + removeObject(bucketName: string, objectName: string, callback: NoResultCallback): void; + removeObject(bucketName: string, objectName: string): Promise; + + removeIncompleteUpload(bucketName: string, objectName: string, callback: NoResultCallback): void; + removeIncompleteUpload(bucketName: string, objectName: string): Promise; + + // Presigned operations + presignedUrl(httpMethod: string, bucketName: string, objectName: string, callback: ResultCallback): void; + presignedUrl(httpMethod: string, bucketName: string, objectName: string, expiry: number, callback: ResultCallback): void; + presignedUrl(httpMethod: string, bucketName: string, objectName: string, expiry: number, reqParams: { [key: string]: any; }, callback: ResultCallback): void; + presignedUrl(httpMethod: string, bucketName: string, objectName: string, expiry?: number, reqParams?: { [key: string]: any; }): Promise; + + presignedGetObject(bucketName: string, objectName: string, callback: ResultCallback): void; + presignedGetObject(bucketName: string, objectName: string, expiry: number, callback: ResultCallback): void; + presignedGetObject(bucketName: string, objectName: string, expiry?: number): Promise; + + presignedPutObject(bucketName: string, objectName: string, callback: ResultCallback): void; + presignedPutObject(bucketName: string, objectName: string, expiry: number, callback: ResultCallback): void; + presignedPutObject(bucketName: string, objectName: string, expiry?: number): Promise; + + presignedPostPolicy(policy: PostPolicy, callback: ResultCallback): void; + presignedPostPolicy(policy: PostPolicy): Promise; + + // Bucket Policy & Notification operations + getBucketNotification(bucketName: string, callback: ResultCallback): void; + getBucketNotification(bucketName: string): Promise; + + setBucketNotification(bucketName: string, bucketNotificationConfig: NotificationConfig, callback: NoResultCallback): void; + setBucketNotification(bucketName: string, bucketNotificationConfig: NotificationConfig): Promise; + + removeAllBucketNotification(bucketName: string, callback: NoResultCallback): void; + removeAllBucketNotification(bucketName: string): Promise; + + // todo #low Specify events + listenBucketNotification(bucketName: string, prefix: string, suffix: string, events: string[]): EventEmitter; + + getBucketPolicy(bucketName: string, objectPrefix: string, callback: ResultCallback): void; + getBucketPolicy(bucketName: string, objectPrefix: string): Promise; + + setBucketPolicy(bucketName: string, objectPrefix: string, bucketPolice: PolicyValue, callback: NoResultCallback): void; + setBucketPolicy(bucketName: string, objectPrefix: string, bucketPolice: PolicyValue): Promise; + + // Other + newPostPolicy(): PostPolicy; +} + +export namespace Policy { + const NONE: 'none'; + const READONLY: 'readonly'; + const WRITEONLY: 'writeonly'; + const READWRITE: 'readwrite'; +} + +export class CopyConditions { + setModified(date: Date): void; + setUnmodified(date: Date): void; + setMatchETag(etag: string): void; + setMatchETagExcept(etag: string): void; +} + +export class PostPolicy { + setExpires(date: Date): void; + setKey(objectName: string): void; + setKeyStartsWith(prefix: string): void; + setBucket(bucketName: string): void; + setContentType(type: string): void; + setContentLengthRange(min: number, max: number): void; +} + +export class NotificationPoller extends EventEmitter { + stop(): void; + start(): void; + // must to be public? + checkForChanges(): void; +} + +export class NotificationConfig { + add(target: TopicConfig|QueueConfig|CloudFunctionConfig): void; +} + +export class TopicConfig extends TargetConfig { + constructor(arn: string); +} + +export class QueueConfig extends TargetConfig { + constructor(arn: string); +} + +export class CloudFunctionConfig extends TargetConfig { + constructor(arn: string); +} + +export function buildARN(partition: string, service: string, region: string, accountId: string, resource: string): string; + +export const ObjectCreatedAll: string; // s3:ObjectCreated:*' +export const ObjectCreatedPut: string; // s3:ObjectCreated:Put +export const ObjectCreatedPost: string; // s3:ObjectCreated:Post +export const ObjectCreatedCopy: string; // s3:ObjectCreated:Copy +export const ObjectCreatedCompleteMultipartUpload: string; // sh:ObjectCreated:CompleteMultipartUpload +export const ObjectRemovedAll: string; // s3:ObjectRemoved:* +export const ObjectRemovedDelete: string; // s3:ObjectRemoved:Delete +export const ObjectRemovedDeleteMarkerCreated: string; // s3:ObjectRemoved:DeleteMarkerCreated +export const ObjectReducedRedundancyLostObject: string; // s3:ReducedRedundancyLostObject diff --git a/types/minio/minio-tests.ts b/types/minio/minio-tests.ts new file mode 100644 index 0000000000..56e81762ce --- /dev/null +++ b/types/minio/minio-tests.ts @@ -0,0 +1,114 @@ +import { Stream } from 'stream'; +import Minio = require('minio'); + +const minio = new Minio.Client({ + endPoint: 'localhost', + port: 9000, + secure: false, + accessKey: 'iV7RAFOtxF', + secretKey: 'Go1hhOkXnl', +}); + +minio.makeBucket('testBucket', (error: Error|null) => { console.log(error); }); +minio.makeBucket('testBucket', 'ap-southeast-2', (error: Error|null) => { console.log(error); }); +minio.makeBucket('testBucket'); +minio.makeBucket('testBucket', 'eu-west-1'); + +minio.listBuckets((error: Error|null, bucketList: Minio.BucketItemFromList[]) => { console.log(error, bucketList); }); +minio.listBuckets(); + +minio.bucketExists('testBucket', (error: Error|null) => { console.log(error); }); +minio.bucketExists('testBucket'); + +minio.removeBucket('testBucket', (error: Error|null) => { console.log(error); }); +minio.removeBucket('testBucket'); + +minio.listObjects('testBucket'); +minio.listObjects('testBucket', 'image_'); +minio.listObjects('testBucket', 'audio_', true); +minio.listObjectsV2('testBucket'); +minio.listObjectsV2('testBucket', 'image_'); +minio.listObjectsV2('testBucket', 'audio_', true); + +minio.listIncompleteUploads('testBucket'); +minio.listIncompleteUploads('testBucket', 'image_'); +minio.listIncompleteUploads('testBucket', 'audio_', true); + +minio.getObject('testBucket', 'hello.jpg', (error: Error|null, objectStream: Stream) => { console.log(error, objectStream); }); +minio.getObject('testBucket', 'hello.jpg'); + +minio.getPartialObject('testBucket', 'hello.jpg', 10, (error: Error|null, objectStream: Stream) => { console.log(error, objectStream); }); +minio.getPartialObject('testBucket', 'hello.jpg', 10, 20, (error: Error|null, objectStream: Stream) => { console.log(error, objectStream); }); +minio.getPartialObject('testBucket', 'hello.jpg', 10); +minio.getPartialObject('testBucket', 'hello.jpg', 10, 20); + +minio.fGetObject('testBucket', 'hello.jpg', 'file/path', (error: Error|null) => { console.log(error); }); +minio.fGetObject('testBucket', 'hello.jpg', 'file/path'); + +minio.putObject('testBucket', 'hello.jpg', new Stream(), (error: Error|null, etag: string) => { console.log(error, etag); }); +minio.putObject('testBucket', 'hello.jpg', new Buffer('string'), 100, (error: Error|null, etag: string) => { console.log(error, etag); }); +minio.putObject('testBucket', 'hello.txt', 'hello.txt content', 100, 'text/plain', (error: Error|null, etag: string) => { console.log(error, etag); }); +minio.putObject('testBucket', 'hello.jpg', new Stream()); +minio.putObject('testBucket', 'hello.jpg', new Buffer('string'), 100); +minio.putObject('testBucket', 'hello.txt', 'hello.txt content', 100, 'text/plain'); + +minio.fPutObject('testBucket', 'hello.jpg', 'file/path', 'image/jpg', (error: Error|null, etag: string) => { console.log(error, etag); }); +minio.fPutObject('testBucket', 'hello.jpg', 'file/path', 'image/jpg'); + +const conditions = new Minio.CopyConditions(); +conditions.setMatchETag('bd891862ea3e22c93ed53a098218791d'); +minio.copyObject('testBucket', 'copy-of-hello.jpg', 'hello.jpg', conditions, (error: Error|null, item: Minio.BucketItemCopy) => { console.log(error, item); }); +minio.copyObject('testBucket', 'copy-of-hello.jpg', 'hello.jpg', conditions); + +minio.statObject('testBucket', 'hello.jpg', (error: Error|null, stat: Minio.BucketItemStat) => { console.log(error, stat); }); +minio.statObject('testBucket', 'hello.jpg'); + +minio.removeObject('testBucket', 'hello.jpg', (error: Error|null) => { console.log(error); }); +minio.removeObject('testBucket', 'hello.jpg'); + +minio.removeIncompleteUpload('testBucket', 'hello.jpg', (error: Error|null) => { console.log(error); }); +minio.removeIncompleteUpload('testBucket', 'hello.jpg'); + +minio.presignedUrl('GET', 'testBucket', 'hello.jpg', (error: Error|null, url: string) => { console.log(error, url); }); +minio.presignedUrl('GET', 'testBucket', 'hello.jpg', 84600, (error: Error|null, url: string) => { console.log(error, url); }); +minio.presignedUrl('GET', 'testBucket', 'hello.jpg', 84600, { prefix: 'data', 'max-keys': 1000 }, (error: Error|null, url: string) => { console.log(error, url); }); +minio.presignedUrl('GET', 'testBucket', 'hello.jpg'); +minio.presignedUrl('GET', 'testBucket', 'hello.jpg', 84600); +minio.presignedUrl('GET', 'testBucket', 'hello.jpg', 84600, { prefix: 'data', 'max-keys': 1000 }); + +minio.presignedGetObject('testBucket', 'hello.jpg', (error: Error|null, url: string) => { console.log(error, url); }); +minio.presignedGetObject('testBucket', 'hello.jpg', 84600, (error: Error|null, url: string) => { console.log(error, url); }); +minio.presignedGetObject('testBucket', 'hello.jpg'); +minio.presignedGetObject('testBucket', 'hello.jpg', 84600); + +minio.presignedPutObject('testBucket', 'hello.jpg', (error: Error|null, url: string) => { console.log(error, url); }); +minio.presignedPutObject('testBucket', 'hello.jpg', 84600, (error: Error|null, url: string) => { console.log(error, url); }); +minio.presignedPutObject('testBucket', 'hello.jpg'); +minio.presignedPutObject('testBucket', 'hello.jpg', 84600); + +const policy = minio.newPostPolicy(); +policy.setBucket('testBucket'); +policy.setKey('hello.jpg'); +minio.presignedPostPolicy(policy, (error: Error|null, data: Minio.PostPolicyResult) => { console.log(error, data); }); +minio.presignedPostPolicy(policy); + +minio.getBucketNotification('testBucket', (error: Error|null, config: Minio.NotificationConfig) => { console.log(error, config); }); +minio.getBucketNotification('testBucket'); + +const notificationConfig = new Minio.NotificationConfig(); +const arn: string = Minio.buildARN('aws', 'sns', 'us-west-2', '408065449417', 'resource'); +const topic = new Minio.TopicConfig(arn); +notificationConfig.add(topic); +minio.setBucketNotification('testBucket', notificationConfig, (error: Error|null) => { console.log(error); }); +minio.setBucketNotification('testBucket', notificationConfig); + +minio.removeAllBucketNotification('testBucket', (error: Error|null) => { console.log(error); }); +minio.removeAllBucketNotification('testBucket'); + +minio.listenBucketNotification('testBucket', 'pref_', '_suf', [ Minio.ObjectCreatedAll ]); + +minio.getBucketPolicy('testBucket', 'pref_', (error: Error|null, policy: Minio.PolicyValue) => { console.log(error, policy); }); +minio.getBucketPolicy('testBucket', ''); + +minio.setBucketPolicy('testBucket', '', Minio.Policy.READWRITE, (error: Error|null) => { console.log(error); }); +minio.setBucketPolicy('testBucket', 'pref_', Minio.Policy.WRITEONLY); diff --git a/types/minio/tsconfig.json b/types/minio/tsconfig.json new file mode 100644 index 0000000000..1cf34ed33d --- /dev/null +++ b/types/minio/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "minio-tests.ts" + ] +} diff --git a/types/minio/tslint.json b/types/minio/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/minio/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }