Skip to content

Commit

Permalink
add typescript typings
Browse files Browse the repository at this point in the history
added new gulp task for generating a d.ts file
  • Loading branch information
fredolss committed Apr 14, 2018
1 parent da9618e commit c537aff
Show file tree
Hide file tree
Showing 15 changed files with 1,186 additions and 7,449 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To follow a path of links you typically start at one URL (most often the root UR

Hyper JS does that for you. You just need to tell Hyper JS where it can find the link to follow in each consecutive document and Hyper JS will happily execute the hops from document to document for you and when it's done, hand you the final http response or document, the one you really wanted to have in the first place.

Hyper JS works only in the browser. For now, Hyper JS only supports JSON APIs.
Hyper JS works only in the browser. For now, Hyper JS only supports JSON APIs.

The most basic thing you can do with Hyper JS is to let it start at the root URL of an API, follow some links and pass the resource that is found at the end of this journey back to you. We call this procedure a *"link traversal process"*.

Expand Down
3 changes: 1 addition & 2 deletions dist/hyperjs.min.js

Large diffs are not rendered by default.

55 changes: 48 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ var tsify = require('tsify');
var source = require('vinyl-source-stream');
var tsc = require("typescript");
var glob = require("glob");
var uglify = require('minify-stream');
var fs = require('fs');

/**
* Bundle the code for testing
*/
gulp.task('bundle-tests', function () {

var files = glob.sync('./tests/spec/*.spec.ts');
Expand All @@ -17,20 +22,41 @@ gulp.task('bundle-tests', function () {
.pipe(gulp.dest("./tests/build"));
});

gulp.task('bundle', function () {
var fs = require('fs');
/**
* Create a minfied bundle
*/
gulp.task('bundle-min', function () {

var json = JSON.parse(fs.readFileSync('./package.json'));
var files = glob.sync('./src/*.ts');
var b = browserify({ plugin: [tsify], entries:files, debug:false, standalone:json.name });
var uglify = require('minify-stream');


return b
.bundle()
.pipe(uglify())
.pipe(uglify({ sourceMap: false }))
.pipe(source(json.name +'.min.js'))
.pipe(gulp.dest("./dist"));
});

/**
* Create bundle main file
*/
gulp.task('bundle', function () {

var json = JSON.parse(fs.readFileSync('./package.json'));
var files = glob.sync('./src/*.ts');
var b = browserify({ entries:files, debug:false, standalone:json.name });

return b
.plugin(tsify, { noImplicitAny: true })
.bundle()
.pipe(source(json.name +'.js'))
.pipe(gulp.dest("./"));
});

/**
* Run test once and exit
*/
gulp.task('test', ["bundle-tests"], function(done) {
new Server( {
configFile:__dirname + '/tests/karma.conf.js',
Expand All @@ -39,8 +65,23 @@ gulp.task('test', ["bundle-tests"], function(done) {
});

/**
* Run test once and exit
* Generate d.ts file
*/
gulp.task('dts',function() {

var json = JSON.parse(fs.readFileSync('./package.json'));

require('dts-generator').default({
name: json.name,
project: './src',
main: 'hyperjs/HyperJS',
out: json.name + '.d.ts'
});
});

/**
* Bundle, create d.ts file and run tests
*/
gulp.task('default', ["bundle", "test"], function () {
gulp.task('default', ["bundle","dts","test"], function () {

});
84 changes: 84 additions & 0 deletions hyperjs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
declare module 'hyperjs/errors/HttpError' {
export default class HttpError extends Error {
url: string;
status: number;
statusText: string;
data: any;
constructor(url: string, status: number, statusText: string, data: any);
}

}
declare module 'hyperjs/HttpClient' {
export interface RequestOptions {
method: string;
url: string;
data?: any;
headers?: any;
contentType?: string;
}
export function request(options: RequestOptions): Promise<any>;

}
declare module 'hyperjs/errors/LinkNotFoundError' {
export default class LinkNotFoundError extends Error {
rel: string;
constructor(rel: string);
}

}
declare module 'hyperjs/errors/UrlMustBeSetError' {
export default class UrlMustBeSetError extends Error {
constructor();
}

}
declare module 'hyperjs/errors/ResourceNotLoadedError' {
export default class ResourceNotLoadedError extends Error {
constructor(resourceUrl: string);
}

}
declare module 'hyperjs/TemplateParameters' {
export function setParameters(url: string, templateParameters: any, queryParameters?: any): string;

}
declare module 'hyperjs/HyperJS' {
import { RequestOptions } from 'hyperjs/HttpClient';
export interface ResourceLink {
rel: string;
href: string;
method?: string;
}
export interface Resource<TData = any> {
fetch(parameters?: any): Promise<Resource<TData>>;
data: TData;
action(rel: string, method: string, body?: any, parameters?: any): Promise<any>;
patch(body?: any, parameters?: any): Promise<any>;
post(body?: any, parameters?: any): Promise<any>;
put(body?: any, parameters?: any): Promise<any>;
delete(body?: any, parameters?: any): Promise<any>;
getLink(rel: string): ResourceLink;
hasLink(rel: string): boolean;
followLink<RType = any>(rel: string, templateParameters?: any, queryParameters?: any): Resource<RType>;
}
export interface CustomRequestOptions {
method?: string;
url?: string;
data?: string | object;
headers?: object;
contentType?: string;
}
export interface ResourceBuilder {
withSelfCallback: (callback: (data: any) => string) => ResourceBuilder;
withLinkCallback: (callback: (rel: string, data: any) => string) => ResourceBuilder;
withRequestOptions: (callback: (resource: Resource, options: RequestOptions) => CustomRequestOptions) => ResourceBuilder;
getResource<TData = any>(resourceUrl: string): Resource<TData>;
wrapResource<TData = any>(resource: TData): Resource<TData>;
}
export function builder(): ResourceBuilder;

}
declare module 'hyperjs' {
import main = require('hyperjs/HyperJS');
export = main;
}
Loading

0 comments on commit c537aff

Please sign in to comment.