Skip to content

Commit c5aa983

Browse files
committed
Bug fix and strong typed results
1 parent 1d57616 commit c5aa983

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

freebase/freebase.js

Lines changed: 6 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

freebase/freebase.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ module Minduca.Freebase {
2626

2727
constructor(private auth?: IFreebaseRequestAuthenticationOptions) { }
2828

29-
public topic(mId: string, invokeOptions: IFreebaseInvokeOptions, options?: IFreebaseTopicRequestOptions): any {
29+
public topic(mId: string, invokeOptions: IFreebaseTopicInvokeOptions, options?: IFreebaseTopicRequestOptions): any {
3030
if (!mId || mId == '')
3131
return;
3232

3333
var url: string = this.getTopicUrl(mId, options);
3434
this.invoke(url, invokeOptions);
3535
}
36-
public search(options: IFreebaseSearchRequestOptions, invokeOptions: IFreebaseInvokeOptions): any {
36+
public search(options: IFreebaseSearchRequestOptions, invokeOptions: IFreebaseSearchInvokeOptions): any {
3737

3838
if (!options || !options.query || options.query == '')
3939
return;
@@ -60,20 +60,7 @@ module Minduca.Freebase {
6060
};
6161

6262
$.ajax(request)
63-
.done(function (data: any, textStatus: string, jqXHR: JQueryXHR) {
64-
65-
if (!data)
66-
return options.fail(jqXHR, "error", "that was weird.. Nothing was returned.");
67-
68-
if (data.status == '200 OK') {
69-
return options.done(data, textStatus, jqXHR);
70-
}
71-
else return data.error ?
72-
options.fail(jqXHR, "error", "An error occurred. Please see the returned data for more information", data)
73-
:
74-
options.fail(jqXHR, "error", "Unknown behavior.", data);
75-
76-
})
63+
.done(options.done)
7764
.fail(options.fail)
7865
.always(options.always);
7966
}
@@ -84,6 +71,7 @@ module Minduca.Freebase {
8471
private getSearchUrl(options: IFreebaseSearchRequestOptions): string { return this.buildServiceRequestUrl('search', options); }
8572
private getTopicUrl(mId: string, options?: IFreebaseTopicRequestOptions) { return this.buildServiceRequestUrl('topic', options, mId); }
8673
private getImageUrl(mid: string, options?: IFreebaseImageRequestOptions): string { return this.buildServiceRequestUrl('image', options, mid); }
74+
private getBaseUrl(): string { return 'https://www.googleapis.com/freebase/v1/'; }
8775

8876
private buildServiceRequestUrl(serviceRelativePath: string, jsonQS?: IFreebaseRequestOptionsBase, ...pathsVariables: string[]): string {
8977

@@ -104,11 +92,11 @@ module Minduca.Freebase {
10492

10593
if (pathsVariables && pathsVariables.length > 0) {
10694
paths = pathsVariables.join("/");
107-
if ((<any>paths.match("^/")) == "/")
95+
if ((<any>paths.match("^/")) != "/")
10896
paths = "/" + paths;
10997
}
11098

111-
return 'https://www.googleapis.com/freebase/v1/'.concat(serviceRelativePath, paths, qs);
99+
return this.getBaseUrl().concat(serviceRelativePath, paths, qs);
112100
}
113101
}
114102

@@ -168,10 +156,17 @@ module Minduca.Freebase {
168156
raw?: boolean;
169157
}
170158

171-
export interface IFreebaseInvokeOptions {
159+
export interface IFreebaseSearchInvokeOptions extends IFreebaseInvokeOptions {
172160
done(data: IFreebaseSearchResult, textStatus: string, jqXHR: JQueryXHR): any;
161+
}
162+
export interface IFreebaseTopicInvokeOptions extends IFreebaseInvokeOptions {
163+
done(data: IFreebaseTopicResultProperty, textStatus: string, jqXHR: JQueryXHR): any;
164+
}
165+
166+
export interface IFreebaseInvokeOptions {
167+
done(data: any, textStatus: string, jqXHR: JQueryXHR): any;
173168
fail? (jqXHR: JQueryXHR, textStatus: string, errorThrow: string, data?: any): any;
174-
fail? (jqXHR: JQueryXHR, textStatus: string, errorThrow: string, data?: IFreebaseSearchResultError): any;
169+
fail? (jqXHR: JQueryXHR, textStatus: string, errorThrow: string, data?: IFreebaseResultError): any;
175170
always? (jqXHR: JQueryXHR, textStatus: string): any;
176171
async?: boolean;
177172
}
@@ -183,6 +178,7 @@ module Minduca.Freebase {
183178
cursor: number;
184179
cost: number;
185180
hits: number;
181+
output?: { [prop: string]: any };
186182
}
187183

188184
export interface IFreebaseSearchResultItem {
@@ -198,7 +194,7 @@ module Minduca.Freebase {
198194
output?: any;
199195
}
200196

201-
export interface IFreebaseSearchResultError {
197+
export interface IFreebaseResultError {
202198
error:
203199
{
204200
errors: {

0 commit comments

Comments
 (0)