-
-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Extract common http methods
- Loading branch information
vakrilov
committed
Oct 3, 2017
1 parent
cc5f210
commit 557510e
Showing
5 changed files
with
118 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NSFileSystem } from "../file-system/ns-file-system"; | ||
|
||
import { Observable } from "rxjs/Observable"; | ||
import { Observer } from "rxjs/Observer"; | ||
import { path } from "tns-core-modules/file-system/file-system"; | ||
|
||
export type httpResponseFactory<T> = (url: string, body: any, status: number) => T; | ||
export type httpErrorFactory = (url: string, body: any, status: number) => any; | ||
|
||
export function isLocalRequest(url: string): boolean { | ||
return url.indexOf("~") === 0 || url.indexOf("/") === 0; | ||
} | ||
|
||
export function getAbsolutePath(url: string, nsFileSystem: NSFileSystem): string { | ||
url = url.replace("~", "").replace("/", ""); | ||
url = path.join(nsFileSystem.currentApp().path, url); | ||
return url; | ||
} | ||
|
||
export function handleLocalRequest<T>( | ||
url: string, | ||
nsFileSystem: NSFileSystem, | ||
successResponse: httpResponseFactory<T>, | ||
errorResponse: httpErrorFactory): Observable<T> { | ||
|
||
url = getAbsolutePath(url, nsFileSystem); | ||
|
||
// request from local app resources | ||
return new Observable((observer: Observer<T>) => { | ||
if (nsFileSystem.fileExists(url)) { | ||
const localFile = nsFileSystem.fileFromPath(url); | ||
localFile.readText() | ||
.then((data) => { | ||
try { | ||
const json = JSON.parse(data); | ||
observer.next(successResponse(url, json, 200)); | ||
observer.complete(); | ||
} catch (error) { | ||
// Even though the response status was 2xx, this is still an error. | ||
// The parse error contains the text of the body that failed to parse. | ||
const errorResult = { error, text: data }; | ||
observer.error(errorResponse(url, errorResult, 200)); | ||
} | ||
}, (err: Object) => { | ||
observer.error(errorResponse(url, err, 400)); | ||
|
||
}); | ||
} else { | ||
observer.error(errorResponse(url, "Not Found", 404)); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.