Skip to content

Commit

Permalink
Revert "Revert "add gitignore gitattributes""
Browse files Browse the repository at this point in the history
This reverts commit fab9e75.
  • Loading branch information
mosheduminer committed Feb 5, 2020
1 parent 5007edd commit b84d891
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
38 changes: 38 additions & 0 deletions auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Axios from 'axios'

import { updateData } from './utils'

export default class Auth {
constructor(private loginInfo: LoginInfo, private sirixInfo: SirixInfo, private authData: AuthData) {
this.authenticate().then(() => {
this.setRefreshTimeout();
});
}
private async authenticate() {
let res = await Axios.post(`${this.sirixInfo.sirixUri}/token`,
{ username: this.loginInfo.username, password: this.loginInfo.password, grant_type: 'password' },
{ headers: { 'Content-Type': 'multipart/form-data' } });
if (res.status >= 400) {
console.error(res.status, res.data);
} else {
updateData(JSON.parse(res.data), this.authData);
}
}
private setRefreshTimeout() {
setTimeout(() => this.refresh(), this.authData.expires_in - 5);
}
private async refresh() {
let res = await Axios.post(`${this.sirixInfo.sirixUri}/token`,
{ refresh_token: this.authData.refresh_token, grant_type: 'refresh_token' },
{ headers: { 'Content-Type': 'multipart/form-data' } });
if (res.status >= 400) {
console.error(res.status, res.data);
await this.authenticate();
this.setRefreshTimeout();
} else {
let authData: AuthData = JSON.parse(res.data);
updateData(authData, this.authData);
this.setRefreshTimeout();
}
}
}
62 changes: 62 additions & 0 deletions client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Axios from 'axios';

import Auth from './auth';
import Database from './database'

export default class Sirix {
constructor(username, password, sirixUri) {
this.sirixInfo.sirixUri = sirixUri;
// initialize with null, so as to fit with the interface
this.authData = {
access_token: null,
expires_in: null,
refresh_expires_in: null,
refresh_token: null,
token_type: null,
not_before_policy: null,
session_state: null,
scope: null
}
new Auth({ username, password, clientId: 'sirix' }, this.sirixInfo, this.authData);
}
private sirixInfo: SirixInfo;
private authData: AuthData;
/**
* database
*/
public async database(db_name: string, db_type: string = null): Promise<Database> {
return new Database(db_name, db_type, this.sirixInfo, this.authData);
}
/**
* get_info
*/
public async get_info(): Promise<DatabaseInfo[]> {
let res = await Axios.get(this.sirixInfo.sirixUri,
{
params: { withResources: true },
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.authData.access_token}`
}
});
if (res.status >= 400) {
console.error(res.status, res.data);
return null;
}
this.sirixInfo.databaseInfo = JSON.parse(res.data);
return this.sirixInfo.databaseInfo;
}
/**
* delete
*/
public async delete(): Promise<boolean> {
let res = await Axios.delete(this.sirixInfo.sirixUri,
{ headers: { Authorization: this.authData.access_token } });
if (res.status >= 400) {
console.error(res.status, res.data);
return false;
}
await this.get_info();
return true;
}
}
49 changes: 49 additions & 0 deletions database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Axios from 'axios';

import { contentType, updateData } from './utils';

export default class Database {
constructor(private name: string, private type: string, private sirixInfo: SirixInfo, private authData: AuthData) {
let db = sirixInfo.databaseInfo.filter(obj => obj.name === name);
if (db.length > 0) {
this.type = db[0].type;
this.exists = true;
}
}
public exists: boolean = false;
/**
* delete
*/
public async delete(): Promise<boolean> {
let res = await Axios.delete(`${this.sirixInfo.sirixUri}/${this.name}`,
{ headers: { Authorization: this.authData.access_token, 'Content-Type': contentType(this.type) } }
);
if (res.status !== 204) {
console.error(res.status, res.data);
return false;
} else {

return true;
}
}
/**
* resource
*/
public resource() {

}
private async create(): Promise<boolean> {
let res = await Axios.put(`${this.sirixInfo.sirixUri}/${this.name}`, {},
{
headers:
{ Authorization: `Bearer ${this.authData.access_token}`, 'Content-Type': contentType(this.type) }
})
if (res.status === 201) {
updateData(res.data, this.sirixInfo.databaseInfo);
return true;
} else {
console.error(res.status, res.data);
return false;
}
}
}
44 changes: 44 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>

/*~ This is the module template file. You should rename it to index.d.ts
*~ and place it in a folder with the same name as the module.
*~ For example, if you were writing a file for "super-greeter", this
*~ file should be 'super-greeter/index.d.ts'
*/

/*~ If this module is a UMD module that exposes a global variable 'myLib' when
*~ loaded outside a module loader environment, declare that global here.
*~ Otherwise, delete this declaration.
*/
export as namespace sirix;

/*~ If this module has methods, declare them as functions like so.
*/
export function myMethod(a: string): string;
export function myOtherMethod(a: number): number;

/*~ You can declare types that are available via importing the module */
export interface someType {
name: string;
length: number;
extras?: string[];
}

/*~ You can declare properties of the module using const, let, or var */
export const myField: number;

/*~ If there are types, properties, or methods inside dotted names
*~ of the module, declare them inside a 'namespace'.
*/
export namespace subProp {
/*~ For example, given this definition, someone could write:
*~ import { subProp } from 'yourModule';
*~ subProp.foo();
*~ or
*~ import * as yourMod from 'yourModule';
*~ yourMod.subProp.foo();
*/
export function foo(): void;
}
Empty file added index.ts
Empty file.
27 changes: 27 additions & 0 deletions info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interface SirixInfo {
sirixUri: string
databaseInfo?: DatabaseInfo[]
}

interface DatabaseInfo {
name: string
type: string
resources: string[]
}

interface LoginInfo {
username: string
password: string
clientId: string
}

interface AuthData {
access_token: string
expires_in: number
refresh_expires_in: number
refresh_token: string
token_type: string
not_before_policy: number
session_state: string
scope: string
}
37 changes: 37 additions & 0 deletions package-lock.json

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

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "sirix-typescript-client",
"version": "1.0.0",
"description": "A typescript client for interacting with SirixDB",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2"
}
}
Loading

0 comments on commit b84d891

Please sign in to comment.