Skip to content

Commit

Permalink
Merge pull request #1 from bosim/http-errors
Browse files Browse the repository at this point in the history
add possibility to check on http errors when using the restful part of the library
  • Loading branch information
druppy authored Mar 26, 2019
2 parents 82e03de + f7fa7e3 commit 5712b57
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// define how the module will look like
import { Session } from './session'
import { RestEntityBase } from './restful'
import { rpc_sess, rpc_names_sess, fetch_smd_sess, Members } from './rpc'
import { RestEntityBase, Options } from './restful'
import { rpc_sess, rpc_names_sess, fetch_smd_sess, Methods } from './rpc'
import {LocaleFn} from './common'
export {Entity, Iter, WatchCallback} from './store'
export {RestIter} from './restful'
Expand Down Expand Up @@ -46,8 +46,12 @@ export function rpc_names() {
return rpc_names_sess(global_session)
}

export function fetch_smd() {
export function fetch_smd() : Promise<Methods> {
return fetch_smd_sess(global_session)
}

export class RestEntity<Data, ArgsT> extends RestEntityBase<Data, ArgsT> { }
export class RestEntity<Data, ArgsT> extends RestEntityBase<Data, ArgsT> {
constructor( entity_name: string, options?: Options) {
super(global_session, entity_name, options)
}
}
66 changes: 50 additions & 16 deletions src/restful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ export class RestIter<Data> implements Iter<Data> {
private args: any
private nfn?: NormalizeFn<Data>
private sess: Session
private options: Options

private url: string

constructor( sess: Session, entity_name: string, args: Object, order?: string[], nfn?: NormalizeFn<Data> ) {
constructor( sess: Session, entity_name: string, args: Object, options: Options, order?: string[], nfn?: NormalizeFn<Data> ) {
this.sess = sess
this.entity_name = entity_name
this.args = args
if( nfn != undefined )
this.nfn = nfn
this.options = options
// need order too !!!
this.url = mk_url( `${sess.rest_base_url_get()}/${this.entity_name}`, args, order )
this.reset()
Expand Down Expand Up @@ -112,6 +114,10 @@ export class RestIter<Data> implements Iter<Data> {
cache: 'no-store',
headers: h
}).then((response) => {
if( this.options.http_errors && !response.ok) {
reject( new Error( "HTTP error: " + response.statusText ))
return
}
let start = 0, end = 0, total = -1

if( response.headers.has( 'Content-Range' )) {
Expand Down Expand Up @@ -149,11 +155,17 @@ export class RestIter<Data> implements Iter<Data> {
res.push( this.nfn( d ))

resolve( res )
}).catch(err => {
reject( err )
})
})
}
}

export interface Options {
http_errors: boolean
}

/**
* Define a rest entity store, that can handle normal RESTful json ations, and a query
* that handle paged loading for large result set
Expand All @@ -163,11 +175,17 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
private entity_name: string
private key_name = 'id'
private sess: Session
private options: Options

constructor( sess: Session, entity_name: string ) {
constructor( sess: Session, entity_name: string, options?: Options ) {
this.entity_name = entity_name

this.sess = sess
if(options != undefined) {
this.options = options
} else {
this.options = { http_errors: false }
}
}

// Make needed type conversions and default data
Expand Down Expand Up @@ -201,9 +219,13 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
cache: 'no-store',
headers: this.sess.headers_get()
}).then((res) => {
res.json().then(jdata => {
resolve( this.normalize( jdata ))
})
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
} else {
res.json().then(jdata => {
resolve( this.normalize( jdata ))
})
}
}).catch( err => {
console.error( 'Restful GET error', err );
reject( err )
Expand All @@ -220,9 +242,13 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
body: JSON.stringify( this.de_normalize( data )),
headers: this.sess.headers_get()
}).then(res => {
res.json().then(jdata => {
resolve( jdata )
})
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
} else {
res.json().then(jdata => {
resolve( jdata )
})
}
}).catch( err => {
// console.error( 'Restful PUT error', err )
reject( err )
Expand All @@ -239,9 +265,13 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
body: JSON.stringify( this.de_normalize( data )),
headers: this.sess.headers_get()
}).then((res) => {
res.json().then(jdata => {
resolve( this.normalize( jdata ))
})
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
} else {
res.json().then(jdata => {
resolve( this.normalize( jdata ))
})
}
}).catch( err => {
// console.error( 'Restful POST error', err );
reject( err )
Expand All @@ -257,9 +287,13 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
cache: 'no-store',
headers: this.sess.headers_get()
}).then( res => {
res.json().then(jdata => {
resolve( jdata )
})
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
} else {
res.json().then(jdata => {
resolve( jdata )
})
}
}).catch( err => {
// console.error( 'Restful DELETE error', err );
reject( err )
Expand All @@ -272,6 +306,6 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
}

public query( args: ArgsT, order?: string[] ) : Iter<Data> {
return new RestIter<Data>( this.sess, this.entity_name, args, order, this.normalize )
return new RestIter<Data>( this.sess, this.entity_name, args, this.options, order, this.normalize)
}
}
}
4 changes: 2 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rpc_sess, rpc_names_sess, fetch_smd_sess, Members} from './rpc'
import { rpc_sess, rpc_names_sess, fetch_smd_sess, Methods } from './rpc'
import { RestEntityBase } from './restful';
import { LocaleFn } from './common'

Expand Down Expand Up @@ -67,7 +67,7 @@ export class Session {
return rpc_sess<T>(this, method, ...args)
}

fetch_smd() {
fetch_smd() : Promise<Methods> {
return fetch_smd_sess( this )
}

Expand Down

0 comments on commit 5712b57

Please sign in to comment.