@elastic.io/maester-client
Advanced tools
Comparing version 2.2.0-dev.2 to 3.0.0-dev.1
@@ -1,5 +0,5 @@ | ||
# 2.2.0 (June 4, 2021) | ||
* | ||
# 3.0.0 (June 10, 2021) | ||
* Library interface has been refactored to comply well-known CRUD approach | ||
# 2.0.0 (May 28, 2021) | ||
Initial version of the reworked library | ||
* Initial version of the reworked library |
{ | ||
"name": "@elastic.io/maester-client", | ||
"version": "2.2.0-dev.2", | ||
"version": "3.0.0-dev.1", | ||
"description": "The official object-storage client for sailor-nodejs.", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
213
README.md
# Maester Client | ||
The official object-storage client for elasticio-sailor-nodejs. | ||
The official Elastic.io object-storage client. | ||
## Usage | ||
Note: All the code snippets written in Typescript | ||
### Create client | ||
``` | ||
const Client = require('@elasticio/maester-client'); | ||
import { ObjectStorageWrapper } from '@elastic.io/maester-client/dist/ObjectStorageWrapper'; | ||
const client = new Client('http://maester.local:3002', 'my-token'); | ||
const objectStorage = new ObjectStorageWrapper(this); | ||
``` | ||
### Buckets API | ||
### CRUD operations | ||
Get bucket: | ||
### Create object | ||
The method has the following signature: | ||
``` | ||
const bucket = await client.buckets.get(id); | ||
async createObject(data: object, queryKey?: string, queryValue?: string, ttl?: number) | ||
``` | ||
where | ||
- data - object data to create. *Required* | ||
- queryKey, queryValue - searchable field (see below in `Get objects by query parameter` section). *Optional*, but if queryKey is specified, queryValue must be specified as well. | ||
- ttl - configurable object's time to live, milliseconds. *Optional* | ||
List buckets: | ||
``` | ||
const buckets = await client.buckets.list({ | ||
page: { | ||
number: 1, | ||
size: 25 | ||
} | ||
}); | ||
const obj = await objectStorage.createObject(data, somequeriablefieldKey, somequeriablefieldValue, 60000); | ||
``` | ||
List buckets by external ID: | ||
### Read operations | ||
#### Get object by ID: | ||
The method has the following signature: | ||
``` | ||
const buckets = await client.buckets.list({ | ||
externalId: 'my-external-id' | ||
}); | ||
async lookupObjectById(id: string) | ||
``` | ||
where | ||
- id - Maester internal id of the object to update. E.g. '76380cae-aee3-457a-9029-d971f61e3731'. *Required* | ||
Create bucket: | ||
``` | ||
const bucket = await client.buckets.create({ | ||
objects: ['object-1', 'object-2', ..., 'object-N'], | ||
extrenalId: 'my-external-id | ||
}); | ||
const obj = await objectStorage.lookupObjectById(id); | ||
``` | ||
Update bucket: | ||
As Maester is able to store any data type, the method returns **a raw string**. | ||
You may want to parse JSON or do any other data processing according to object's expected data type: | ||
``` | ||
const bucket = await client.buckets.update(id, { | ||
closed: true | ||
}); | ||
const parsedObject = JSON.parse(obj); | ||
``` | ||
The following errors can be thrown: | ||
- Object Not Found | ||
- Invalid object id | ||
Delete bucket: | ||
#### Get objects by query parameter: | ||
The method has the following signature: | ||
``` | ||
await client.buckets.delete(id); | ||
async lookupObjectByQueryParameter(key: string, value: string) | ||
``` | ||
where | ||
- key, value - searchable field. *Required* | ||
### Objects API | ||
Get object: | ||
If you create an object with a queriable header, internally it looks like this: | ||
``` | ||
const object = await client.objects.get(id); | ||
console.log(object.data); | ||
x-query-somequeriablefieldKey: somequeriablefieldValue | ||
``` | ||
where 'x-query-' is a default prefix. | ||
Object's property `data` has value of type `string`, `object`, `Buffer` or `Stream`. | ||
Get object as JSON: | ||
Using Maester REST API you can find this object by: | ||
``` | ||
const object = await client.objects.getJSON(id); | ||
console.log(object.data); | ||
/objects?query[somequeriablefieldkey]=somequeriablefieldValue | ||
``` | ||
Get object as buffer: | ||
Using the library: | ||
``` | ||
const object = await client.objects.getBuffer(id); | ||
console.log(object.data.toString()) | ||
const obj = await objectStorage.lookupObjectByQueryParameter('somequeriablefieldKey', 'somequeriablefieldValue'); | ||
``` | ||
Get object as stream: | ||
### Update object | ||
The method has the following signature: | ||
``` | ||
const object = await client.objects.getStream(id); | ||
object.data.pipe(...) | ||
async updateObject(id: string, data: object) | ||
``` | ||
where | ||
- id - Maester internal id of the object to update. E.g. '76380cae-aee3-457a-9029-d971f61e3731'. *Required* | ||
- data - object to update. *Required* | ||
Get object query: | ||
``` | ||
const query = { | ||
'x-query-foo': 'fooQuery', | ||
'x-query-bar': 'barQuery', | ||
}; | ||
const response = await this.client.objects.getObjectQuery(query); | ||
const obj = await objectStorage.updateObject(id, data); | ||
``` | ||
Create read stream example: | ||
### Delete object | ||
The method has the following signature: | ||
``` | ||
client.objects.createReadStream(id).pipe(fs.createWriteStream('/foo/bar.jpg')); | ||
async deleteObjectById(id: string) | ||
``` | ||
where | ||
- id - Maester internal id of the object to update. E.g. '76380cae-aee3-457a-9029-d971f61e3731'. *Required* | ||
Create object: | ||
``` | ||
const response = await client.objects.create(data); | ||
const obj = await objectStorage.deleteObjectById(id); | ||
``` | ||
Where `data` can be `string`, `Buffer`, `Stream` or array of these values. | ||
Create object with queryable parameters: | ||
``` | ||
const params = { | ||
objectFields: { | ||
key1: { | ||
Meta: 'someMeta', | ||
Query: 'someQuery', | ||
} | ||
} | ||
} | ||
const response = await client.objects.create(data, params); | ||
``` | ||
Create object with metadata: | ||
``` | ||
const response = await client.objects.create(data, { | ||
metadata: { | ||
key: 'value' | ||
} | ||
}); | ||
``` | ||
Create object and override its content type: | ||
``` | ||
const response = await client.objects.create({ | ||
data: 'hello world', | ||
contentType: 'text/plain' | ||
}); | ||
``` | ||
Create multiple objects at once: | ||
``` | ||
const data = [ | ||
{ | ||
data: 'hello world' | ||
}, | ||
{ | ||
data: JSON.stringify(json), | ||
contentType: 'application/json' | ||
}, | ||
fs.createReadStream('/foo/bar.jpg'), | ||
Buffer.allocUnsafe(1024) | ||
]; | ||
const response = await client.objects.create(data, { | ||
bucket: 'bucket-id', | ||
metadata: { | ||
description: 'my stuff' | ||
} | ||
}); | ||
``` | ||
Writable stream example: | ||
``` | ||
fs.createReadStream('/foo/bar.jpg').pipe(client.objects.createWriteStream()); | ||
``` | ||
Update object query: | ||
``` | ||
const data = 'hello world'; | ||
const objectFields = { | ||
foo: { Query: 'fooQuery', Meta: 'fooMeta' }, | ||
bar: { Query: 'barQuery', Meta: 'barMeta' } | ||
}; | ||
const params = { id: 'some', objectFields }; | ||
const object = await this.client.objects.updateObjectQuery(data, params); | ||
``` | ||
Delete object: | ||
``` | ||
await client.objects.delete(id); | ||
``` | ||
Delete object query: | ||
``` | ||
const query = { | ||
foo: 'a', | ||
bar: 'b' | ||
}; | ||
await client.objects.delete(query); | ||
``` |
@@ -30,2 +30,4 @@ /* eslint-disable no-unused-expressions */ | ||
}; | ||
// eslint-disable-next-line max-len | ||
const responseString = '{"contentLength":"meta.contentLength","contentType":"meta.contentType","createdAt":"meta.createdAt","md5":"meta.md5Hash","objectId":"obj.id","metadata":"meta.userMetadata"}'; | ||
@@ -85,4 +87,3 @@ let sinon: SinonSandbox; | ||
// TODO enable test | ||
xit('should retry get request on errors', async () => { | ||
it('should retry get request on errors', async () => { | ||
const objectStorage = new ObjectStorage(config); | ||
@@ -101,3 +102,3 @@ | ||
expect(objectStorageCalls.isDone()).to.be.true; | ||
expect(response).to.be.deep.equal(responseData); | ||
expect(response).to.be.deep.equal(responseString); | ||
}); | ||
@@ -199,4 +200,3 @@ | ||
// TODO enable test | ||
xit('should retry get request on errors', async () => { | ||
it('should retry get request on errors', async () => { | ||
const objectStorageWithMiddlewares = new ObjectStorage(config); | ||
@@ -219,3 +219,3 @@ objectStorageWithMiddlewares.use(encryptStream, decryptStream); | ||
expect(objectStorageWithMiddlewaresCalls.isDone()).to.be.true; | ||
expect(response).to.be.deep.equal(responseData); | ||
expect(response).to.be.deep.equal(responseString); | ||
}); | ||
@@ -307,4 +307,3 @@ | ||
// TODO enable test | ||
xit('should get 2 objects successfully', async () => { | ||
it('should get 2 objects successfully', async () => { | ||
const objectStorageWithMiddlewares = new ObjectStorage(config); | ||
@@ -331,4 +330,4 @@ objectStorageWithMiddlewares.use(encryptStream, decryptStream); | ||
expect(objectStorageWithMiddlewaresCalls.isDone()).to.be.true; | ||
expect(outStreamFirst).to.be.deep.equal(responseData); | ||
expect(outStreamSecond).to.be.deep.equal(responseData); | ||
expect(outStreamFirst).to.be.deep.equal(responseString); | ||
expect(outStreamSecond).to.be.deep.equal(responseString); | ||
}); | ||
@@ -335,0 +334,0 @@ |
@@ -94,3 +94,3 @@ /* eslint-disable max-len */ | ||
.matchHeader('x-query-baz', queryValue) | ||
.reply(200, createObjectWithQueriableField); | ||
.reply(201, createObjectWithQueriableField); | ||
const result = await objectStorageWrapper.createObject(data, queryKey, queryValue, ttl); | ||
@@ -105,3 +105,3 @@ expect(result).to.deep.equal(createObjectWithQueriableField); | ||
.matchHeader('x-query-baz', queryValue) | ||
.reply(200, createObjectWithoutQueriableField); | ||
.reply(201, createObjectWithoutQueriableField); | ||
const result = await objectStorageWrapper.createObject(data, queryKey, queryValue, ttl); | ||
@@ -108,0 +108,0 @@ expect(result).to.deep.equal(createObjectWithoutQueriableField); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58176
25
1327
105