Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
/dist
node_modules
.env
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 1.2.0 (October 16,2020)

* change structure of CreateObjectResponse
* change structure of GetObjectResponse

# 1.1.0 (October 7, 2020)

* Add query parameters processing

# 1.0.0 (April 17, 2020)

* Basic release of `Maester Client` library
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ The official object-storage client for elasticio-sailor-nodejs.

### Create client
```
const Client = require('@elasticio/maester-client');
const { Client } = require('@elastic.io/maester-client');

const client = new Client('http://maester.local:3002', 'my-token');
```

### Buckets API
### Buckets API (deprecated)

Get bucket:

Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elastic.io/maester-client",
"version": "1.1.0",
"version": "1.2.0-beta",
"description": "The official object-storage client for sailor-nodejs.",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down Expand Up @@ -37,6 +37,7 @@
"@typescript-eslint/eslint-plugin": "2.28.0",
"@typescript-eslint/parser": "2.28.0",
"chai": "4.2.0",
"dotenv": "8.2.0",
"eslint": "6.8.0",
"eslint-config-standard-with-typescript": "15.0.1",
"eslint-plugin-import": "2.20.2",
Expand Down
40 changes: 40 additions & 0 deletions spec-integration/object.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from 'fs';
import dotenv from 'dotenv';
import { expect } from 'chai';
import { describe, it, before } from 'mocha';
import {Client} from "../src";


describe('objects', function () {
before(function () {
if (fs.existsSync('.env')) {
dotenv.config();
}
this.client = new Client(process.env.MAESTER_URL || '', process.env.TOKEN || '') ;
})

describe('get object', function () {
it('should create and get object', async function () {
const data = 'test';
const contentType = 'application/octet-stream';
const contentLength = 4;

const params = {
objectFields: {
key1: {
Meta: 'someMeta',
Query: 'someQuery',
}
}
};
const object = await this.client.objects.create(data, params);
expect(object.contentType).to.equal(contentType);
const response = await this.client.objects.get(object.id);

expect(response.contentType).to.equal(contentType);
expect(response.contentLength).to.equal(contentLength);
expect(response.data).to.deep.equal(data);
expect(response.queriableFields).to.deep.equal({ key1: 'someQuery'});
});
});
});
47 changes: 37 additions & 10 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Readable, Writable, PassThrough } from 'stream';
import { AxiosInstance, AxiosResponse, ResponseType } from 'axios';

export const USER_META_HEADER_PREFIX = 'x-meta-';
export const USER_QUERY_HEADER_PREFIX = 'x-query-';
export const DEFAULT_CONTENT_TYPE = 'application/octet-stream';

export type PlainOrArray<T> = T | T[];
Expand Down Expand Up @@ -36,18 +37,40 @@ export function headersToMeta(headers: Record<string, string>): ObjectMetadata {
return meta;
}

export function headersToQuery(headers: Record<string, string>): ObjectMetadata {
const meta: ObjectMetadata = {};
for (const [key, value] of Object.entries(headers)) {
if (key.indexOf(USER_QUERY_HEADER_PREFIX) === 0) {
meta[key.substr(USER_QUERY_HEADER_PREFIX.length)] = value;
}
}
return meta;
}

export class GetObjectResponse {
public readonly contentType: string;
public readonly contentLength: number;
public readonly metadata: ObjectMetadata;
public readonly data: object | string | Buffer | Readable;
public readonly contentType?: string;
public readonly contentLength?: number;
public readonly metadata?: ObjectMetadata;
public readonly queriableFields?: QueriableField;

constructor(res: AxiosResponse) {
const { headers, data } = res;
this.contentType = headers['content-type'] ?? '';
this.contentLength = parseInt(headers['content-length'] ?? 0);
this.metadata = headersToMeta(headers);
this.data = data;
if (headers['content-type']) {
this.contentType = headers['content-type'];
}
if (headers['content-length']) {
this.contentLength = parseInt(headers['content-length']);
}
const metadata: ObjectMetadata = headersToMeta(headers);
if (Object.keys(metadata).length !== 0) {
this.metadata = metadata;
}
const queriableFields = headersToQuery(headers);
if (Object.keys(queriableFields).length !== 0) {
this.queriableFields = queriableFields;
}
}
}

Expand Down Expand Up @@ -90,17 +113,21 @@ export interface CreateObjectResponseData {
export class CreateObjectResponse {
public readonly id: string;
public readonly contentType: string;
public readonly contentLength: number;
public readonly md5: string;
public readonly contentLength?: number;
public readonly md5?: string;
public readonly createdAt: Date | null;
public readonly metadata: ObjectMetadata;

constructor(data: CreateObjectResponseData) {
const { objectId, contentType, contentLength, md5, createdAt, metadata } = data;
this.id = objectId ?? '';
this.contentType = contentType ?? '';
this.contentLength = contentLength ?? 0;
this.md5 = md5 ?? '';
if (contentLength) {
this.contentLength = contentLength;
}
if (md5) {
this.md5 = md5;
}
this.createdAt = createdAt ? new Date(createdAt) : null;
this.metadata = metadata ?? {};
}
Expand Down