mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
No more ambient modules for tns-core-modules/* subpackages.
- Use path mappings in tsconfig.json to resolve module typings - Only use ambient mobules for global API's - Move single-file modules to a subdir with the same name so that we can provide a hand-written typing next to it (via package.json) - Delete all mentions of tns-core-modules.d.ts - Delete reference d.ts assembly build steps. Not needed anymore. - HACK! Use a <reference> for global typings in application.d.ts to avoid publishing a separate @types/tns-core-modules package. - Rename declarations.d.ts to tns-core-modules.d.ts to preserve JS project mappings in references.d.ts (the only place we use those)
This commit is contained in:
committed by
Hristo Deshev
parent
1af8c6ca8e
commit
b45cbe929b
7
tns-core-modules/http/http-request.d.ts
vendored
7
tns-core-modules/http/http-request.d.ts
vendored
@@ -1,7 +0,0 @@
|
||||
//@private
|
||||
|
||||
declare module "http/http-request" {
|
||||
import { HttpResponse, HttpRequestOptions, Headers } from "http";
|
||||
export var request: (options: HttpRequestOptions) => Promise<HttpResponse>;
|
||||
export function addHeader(headers: Headers, key: string, value: string): void;
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import * as platformModule from "platform";
|
||||
import * as fsModule from "file-system";
|
||||
|
||||
// this is imported for definition purposes only
|
||||
import { Headers, HttpRequestOptions, HttpResponse } from "http";
|
||||
import * as http from "../../http";
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
@@ -63,7 +63,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
}
|
||||
|
||||
// read the headers
|
||||
var headers: Headers = {};
|
||||
var headers: http.Headers = {};
|
||||
if (result.headers) {
|
||||
var jHeaders = result.headers;
|
||||
var length = jHeaders.size();
|
||||
@@ -140,7 +140,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
|
||||
});
|
||||
}
|
||||
|
||||
function buildJavaOptions(options: HttpRequestOptions) {
|
||||
function buildJavaOptions(options: http.HttpRequestOptions) {
|
||||
if (typeof options.url !== "string") {
|
||||
throw new Error("Http request must provide a valid url.");
|
||||
}
|
||||
@@ -183,13 +183,13 @@ function buildJavaOptions(options: HttpRequestOptions) {
|
||||
return javaOptions;
|
||||
}
|
||||
|
||||
export function request(options: HttpRequestOptions): Promise<HttpResponse> {
|
||||
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
|
||||
if (options === undefined || options === null) {
|
||||
// TODO: Shouldn't we throw an error here - defensive programming
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise<HttpResponse>((resolve, reject) => {
|
||||
return new Promise<http.HttpResponse>((resolve, reject) => {
|
||||
try {
|
||||
// initialize the options
|
||||
var javaOptions = buildJavaOptions(options);
|
||||
@@ -222,7 +222,7 @@ function decodeResponse(raw: any, encoding?: HttpResponseEncoding) {
|
||||
return raw.toString(charsetName)
|
||||
}
|
||||
|
||||
export function addHeader(headers: Headers, key: string, value: string): void {
|
||||
export function addHeader(headers: http.Headers, key: string, value: string): void {
|
||||
if (!headers[key]) {
|
||||
headers[key] = value;
|
||||
} else if (Array.isArray(headers[key])) {
|
||||
@@ -232,4 +232,4 @@ export function addHeader(headers: Headers, key: string, value: string): void {
|
||||
values.push(value);
|
||||
headers[key] = values;
|
||||
}
|
||||
}
|
||||
}
|
||||
5
tns-core-modules/http/http-request/http-request.d.ts
vendored
Normal file
5
tns-core-modules/http/http-request/http-request.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
//@private
|
||||
|
||||
import * as http from "..";
|
||||
export var request: (options: http.HttpRequestOptions) => Promise<http.HttpResponse>;
|
||||
export function addHeader(headers: http.Headers, key: string, value: string): void;
|
||||
@@ -2,8 +2,7 @@
|
||||
* iOS specific http request implementation.
|
||||
*/
|
||||
|
||||
import { HttpRequestOptions, HttpResponse, Headers } from "http";
|
||||
|
||||
import * as http from "../../http";
|
||||
import * as types from "utils/types";
|
||||
import * as imageSourceModule from "image-source";
|
||||
import * as fsModule from "file-system";
|
||||
@@ -11,7 +10,7 @@ import * as fsModule from "file-system";
|
||||
import * as utils from "utils/utils";
|
||||
import getter = utils.ios.getter;
|
||||
|
||||
import * as domainDebugger from "./../debugger/debugger";
|
||||
import * as domainDebugger from "../../debugger/debugger";
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
@@ -64,8 +63,8 @@ function ensureImageSource() {
|
||||
}
|
||||
}
|
||||
|
||||
export function request(options: HttpRequestOptions): Promise<HttpResponse> {
|
||||
return new Promise<HttpResponse>((resolve, reject) => {
|
||||
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
|
||||
return new Promise<http.HttpResponse>((resolve, reject) => {
|
||||
|
||||
try {
|
||||
var network = domainDebugger.getNetwork();
|
||||
@@ -106,7 +105,7 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> {
|
||||
if (error) {
|
||||
reject(new Error(error.localizedDescription));
|
||||
} else {
|
||||
var headers: Headers = {};
|
||||
var headers: http.Headers = {};
|
||||
if (response && response.allHeaderFields) {
|
||||
var headerFields = response.allHeaderFields;
|
||||
|
||||
@@ -191,7 +190,7 @@ function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
|
||||
return NSString.alloc().initWithDataEncoding(data, code).toString();
|
||||
}
|
||||
|
||||
export function addHeader(headers: Headers, key: string, value: string): void {
|
||||
export function addHeader(headers: http.Headers, key: string, value: string): void {
|
||||
if (!headers[key]) {
|
||||
headers[key] = value;
|
||||
} else if (Array.isArray(headers[key])) {
|
||||
@@ -201,4 +200,4 @@ export function addHeader(headers: Headers, key: string, value: string): void {
|
||||
values.push(value);
|
||||
headers[key] = values;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
tns-core-modules/http/http-request/package.json
Normal file
7
tns-core-modules/http/http-request/package.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name" : "http-request",
|
||||
"main" : "http-request",
|
||||
"types": "http-request.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
|
||||
235
tns-core-modules/http/http.d.ts
vendored
235
tns-core-modules/http/http.d.ts
vendored
@@ -1,155 +1,152 @@
|
||||
/**
|
||||
* Allows you to send web requests and receive the responses.
|
||||
*/
|
||||
declare module "http" {
|
||||
import * as image from "image-source";
|
||||
import * as fs from "file-system";
|
||||
import * as image from "image-source";
|
||||
import * as fs from "file-system";
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string.
|
||||
* @param url The URL to request from.
|
||||
*/
|
||||
export function getString(url: string): Promise<string>;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string.
|
||||
* @param options An object that specifies various request options.
|
||||
*/
|
||||
export function getString(options: HttpRequestOptions): Promise<string>;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
||||
* @param url The URL to request from.
|
||||
*/
|
||||
export function getJSON<T>(url: string): Promise<T>;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
||||
* @param options An object that specifies various request options.
|
||||
*/
|
||||
export function getJSON<T>(options: HttpRequestOptions): Promise<T>;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to decode it as an image.
|
||||
* @param url The URL to request from.
|
||||
*/
|
||||
export function getImage(url: string): Promise<image.ImageSource>;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to decode it as an image.
|
||||
* @param options An object that specifies various request options.
|
||||
*/
|
||||
export function getImage(options: HttpRequestOptions): Promise<image.ImageSource>;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to save it as file.
|
||||
* @param url The URL to request from.
|
||||
* @param destinationFilePath Optional. The downloaded file path.
|
||||
*/
|
||||
export function getFile(url: string, destinationFilePath?: string): Promise<fs.File>;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to save it as file.
|
||||
* @param options An object that specifies various request options.
|
||||
* @param destinationFilePath Optional. The downloaded file path.
|
||||
*/
|
||||
export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<fs.File>;
|
||||
|
||||
/**
|
||||
* Makes a generic http request using the provided options and returns a HttpResponse Object.
|
||||
* @param options An object that specifies various request options.
|
||||
*/
|
||||
export function request(options: HttpRequestOptions): Promise<HttpResponse>;
|
||||
|
||||
/**
|
||||
* Provides options for the http requests.
|
||||
*/
|
||||
export interface HttpRequestOptions {
|
||||
/**
|
||||
* Gets or sets the request url.
|
||||
*/
|
||||
url: string;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string.
|
||||
* @param url The URL to request from.
|
||||
* Gets or sets the request method.
|
||||
*/
|
||||
export function getString(url: string): Promise<string>
|
||||
method: string;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string.
|
||||
* @param options An object that specifies various request options.
|
||||
* Gets or sets the request headers in JSON format.
|
||||
*/
|
||||
export function getString(options: HttpRequestOptions): Promise<string>
|
||||
headers?: any;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
||||
* @param url The URL to request from.
|
||||
* Gets or sets the request body.
|
||||
*/
|
||||
export function getJSON<T>(url: string): Promise<T>
|
||||
content?: string | FormData;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
||||
* @param options An object that specifies various request options.
|
||||
* Gets or sets the request timeout in milliseconds.
|
||||
*/
|
||||
export function getJSON<T>(options: HttpRequestOptions): Promise<T>
|
||||
timeout?: number;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to decode it as an image.
|
||||
* @param url The URL to request from.
|
||||
* Gets or sets wether to *not* follow server's redirection responses.
|
||||
*/
|
||||
export function getImage(url: string): Promise<image.ImageSource>
|
||||
dontFollowRedirects?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates HTTP-response information from an HTTP-request.
|
||||
*/
|
||||
export interface HttpResponse {
|
||||
/**
|
||||
* Gets the response status code.
|
||||
*/
|
||||
statusCode: number;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to decode it as an image.
|
||||
* @param options An object that specifies various request options.
|
||||
* Gets the response headers.
|
||||
*/
|
||||
export function getImage(options: HttpRequestOptions): Promise<image.ImageSource>
|
||||
headers: Headers;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to save it as file.
|
||||
* @param url The URL to request from.
|
||||
* @param destinationFilePath Optional. The downloaded file path.
|
||||
* Gets the response content.
|
||||
*/
|
||||
export function getFile(url: string, destinationFilePath?: string): Promise<fs.File>
|
||||
content?: HttpContent;
|
||||
}
|
||||
|
||||
export type Headers = { [key: string]: string | string[] };
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
/**
|
||||
* Encapsulates the content of an HttpResponse.
|
||||
*/
|
||||
export interface HttpContent {
|
||||
/**
|
||||
* Gets the response body as raw data.
|
||||
*/
|
||||
raw: any;
|
||||
|
||||
/**
|
||||
* Downloads the content from the specified URL and attempts to save it as file.
|
||||
* @param options An object that specifies various request options.
|
||||
* @param destinationFilePath Optional. The downloaded file path.
|
||||
* Gets the response body as string.
|
||||
*/
|
||||
export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<fs.File>
|
||||
toString: (encoding?: HttpResponseEncoding) => string;
|
||||
|
||||
/**
|
||||
* Makes a generic http request using the provided options and returns a HttpResponse Object.
|
||||
* @param options An object that specifies various request options.
|
||||
* Gets the response body as JSON object.
|
||||
*/
|
||||
export function request(options: HttpRequestOptions): Promise<HttpResponse>;
|
||||
toJSON: (encoding?: HttpResponseEncoding) => any;
|
||||
|
||||
/**
|
||||
* Provides options for the http requests.
|
||||
* Gets the response body as ImageSource.
|
||||
*/
|
||||
export interface HttpRequestOptions {
|
||||
/**
|
||||
* Gets or sets the request url.
|
||||
*/
|
||||
url: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the request method.
|
||||
*/
|
||||
method: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the request headers in JSON format.
|
||||
*/
|
||||
headers?: any;
|
||||
|
||||
/**
|
||||
* Gets or sets the request body.
|
||||
*/
|
||||
content?: string | FormData;
|
||||
|
||||
/**
|
||||
* Gets or sets the request timeout in milliseconds.
|
||||
*/
|
||||
timeout?: number;
|
||||
|
||||
/**
|
||||
* Gets or sets wether to *not* follow server's redirection responses.
|
||||
*/
|
||||
dontFollowRedirects?: boolean;
|
||||
}
|
||||
toImage: () => Promise<image.ImageSource>;
|
||||
|
||||
/**
|
||||
* Encapsulates HTTP-response information from an HTTP-request.
|
||||
* Gets the response body as file.
|
||||
*/
|
||||
export interface HttpResponse {
|
||||
/**
|
||||
* Gets the response status code.
|
||||
*/
|
||||
statusCode: number;
|
||||
|
||||
/**
|
||||
* Gets the response headers.
|
||||
*/
|
||||
headers: Headers;
|
||||
|
||||
/**
|
||||
* Gets the response content.
|
||||
*/
|
||||
content?: HttpContent;
|
||||
}
|
||||
|
||||
export type Headers = { [key: string]: string | string[] };
|
||||
|
||||
export const enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates the content of an HttpResponse.
|
||||
*/
|
||||
export interface HttpContent {
|
||||
/**
|
||||
* Gets the response body as raw data.
|
||||
*/
|
||||
raw: any;
|
||||
|
||||
/**
|
||||
* Gets the response body as string.
|
||||
*/
|
||||
toString: (encoding?: HttpResponseEncoding) => string;
|
||||
|
||||
/**
|
||||
* Gets the response body as JSON object.
|
||||
*/
|
||||
toJSON: (encoding?: HttpResponseEncoding) => any;
|
||||
|
||||
/**
|
||||
* Gets the response body as ImageSource.
|
||||
*/
|
||||
toImage: () => Promise<image.ImageSource>;
|
||||
|
||||
/**
|
||||
* Gets the response body as file.
|
||||
*/
|
||||
toFile: (destinationFilePath?: string) => fs.File;
|
||||
}
|
||||
}
|
||||
toFile: (destinationFilePath?: string) => fs.File;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as image from "image-source";
|
||||
import * as httpRequest from "http/http-request";
|
||||
import * as httpRequest from "./http-request";
|
||||
|
||||
global.moduleMerge(httpRequest, exports);
|
||||
|
||||
@@ -49,4 +49,4 @@ export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
|
||||
}
|
||||
}, e => reject(e));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"name" : "http",
|
||||
"main" : "http",
|
||||
"nativescript": {}
|
||||
"name" : "http",
|
||||
"main" : "http",
|
||||
"types": "http.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user