Skip to content

Commit

Permalink
[*] dependencies update
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugic committed Mar 2, 2017
1 parent d283367 commit c9eaba6
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 110 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -122,6 +122,9 @@ You can find full list of interfaces [here](ts/Interfaces.ts)

## Changelog

## 2.0.0-beta.18
* some minor typings changes

## 2.0.0-beta.17
* dependencies updated
* webrtc typings removed (now part of typescript default lib)
Expand Down
12 changes: 6 additions & 6 deletions demo/typescript-project/package.json
@@ -1,14 +1,14 @@
{
"name": "api-ai-javascript-typescript-demo-project",
"version": "2.0.0-beta.17",
"version": "2.0.0-beta.18",
"description": "Javascript SDK for https://api.ai/ typescript demo project",
"devDependencies": {
"@types/es6-promise": "0.0.32",
"api-ai-javascript": "^2.0.0-beta.17",
"awesome-typescript-loader": "^3.0.0-beta.18",
"typescript": "^2.1.5",
"webpack": "^2.2.0",
"webpack-dev-server": "^2.2.0"
"api-ai-javascript": "^2.0.0-beta.18",
"awesome-typescript-loader": "^3.0.8",
"typescript": "^2.2.1",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
},
"scripts": {
"build": "webpack",
Expand Down
2 changes: 1 addition & 1 deletion es6/ApiAiConstants.js
Expand Up @@ -18,7 +18,7 @@ export var ApiAiConstants;
AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["RU"] = "ru"] = "RU";
AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["UK"] = "uk"] = "UK";
})(AVAILABLE_LANGUAGES = ApiAiConstants.AVAILABLE_LANGUAGES || (ApiAiConstants.AVAILABLE_LANGUAGES = {}));
ApiAiConstants.VERSION = "2.0.0-beta.16";
ApiAiConstants.VERSION = "2.0.0-beta.18";
ApiAiConstants.DEFAULT_BASE_URL = "https://api.api.ai/v1/";
ApiAiConstants.DEFAULT_API_VERSION = "20150910";
ApiAiConstants.DEFAULT_CLIENT_LANG = AVAILABLE_LANGUAGES.EN;
Expand Down
2 changes: 1 addition & 1 deletion es6/Request/Request.js
Expand Up @@ -16,7 +16,7 @@ class Request {
return Promise.resolve(JSON.parse(xhr.responseText));
}
static handleError(xhr) {
let error = null;
let error = new ApiAiRequestError(null);
try {
const serverResponse = JSON.parse(xhr.responseText);
if (serverResponse.status && serverResponse.status.errorDetails) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "api-ai-javascript",
"version": "2.0.0-beta.17",
"version": "2.0.0-beta.18",
"description": "Javascript SDK for https://api.ai/",
"main": "index",
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions target/ApiAi.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion target/ApiAi.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions target/ApiAi.streamless.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion target/ApiAi.streamless.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ts/ApiAiConstants.ts
Expand Up @@ -5,7 +5,7 @@ export namespace ApiAiConstants {
JA = "ja" as any, KO = "ko" as any, PT = "pt" as any, RU = "ru" as any, UK = "uk" as any
}

export const VERSION: string = "2.0.0-beta.17";
export const VERSION: string = "2.0.0-beta.18";
export const DEFAULT_BASE_URL: string = "https://api.api.ai/v1/";
export const DEFAULT_API_VERSION: string = "20150910";
export const DEFAULT_CLIENT_LANG: AVAILABLE_LANGUAGES = AVAILABLE_LANGUAGES.EN;
Expand Down
71 changes: 36 additions & 35 deletions ts/Request/Request.ts
Expand Up @@ -5,51 +5,52 @@ import XhrRequest from "../XhrRequest";

abstract class Request {

private static handleSuccess(xhr: XMLHttpRequest): Promise<IServerResponse> {
return Promise.resolve(JSON.parse(xhr.responseText));
private static handleSuccess(xhr: XMLHttpRequest): Promise<IServerResponse> {
return Promise.resolve(JSON.parse(xhr.responseText));
}

private static handleError(xhr: XMLHttpRequest): Promise<ApiAiRequestError> {

let error = new ApiAiRequestError(null);
try {
const serverResponse: IServerResponse = JSON.parse(xhr.responseText);
if (serverResponse.status && serverResponse.status.errorDetails) {
error = new ApiAiRequestError(serverResponse.status.errorDetails, serverResponse.status.code);
} else {
error = new ApiAiRequestError(xhr.statusText, xhr.status);
}
} catch (e) {
error = new ApiAiRequestError(xhr.statusText, xhr.status);
}

private static handleError(xhr: XMLHttpRequest): Promise<IServerResponse> {

let error = null;
try {
const serverResponse: IServerResponse = JSON.parse(xhr.responseText);
if (serverResponse.status && serverResponse.status.errorDetails) {
error = new ApiAiRequestError(serverResponse.status.errorDetails, serverResponse.status.code);
} else {
error = new ApiAiRequestError(xhr.statusText, xhr.status);
}
} catch (e) {
error = new ApiAiRequestError(xhr.statusText, xhr.status);
}
return Promise.reject(error);
}
return Promise.reject(error);
}

protected uri;
protected requestMethod;
protected headers;
protected uri;
protected requestMethod;
protected headers;

constructor(protected apiAiClient: ApiAiClient, protected options: IRequestOptions) {
constructor(protected apiAiClient: ApiAiClient, protected options: IRequestOptions) {

this.uri = this.apiAiClient.getApiBaseUrl() + "query?v=" + this.apiAiClient.getApiVersion();
this.requestMethod = XhrRequest.Method.POST;
this.headers = {
Authorization: "Bearer " + this.apiAiClient.getAccessToken(),
};
this.uri = this.apiAiClient.getApiBaseUrl() + "query?v=" + this.apiAiClient.getApiVersion();
this.requestMethod = XhrRequest.Method.POST;
this.headers = {
Authorization: "Bearer " + this.apiAiClient.getAccessToken(),
};

this.options.lang = this.apiAiClient.getApiLang();
this.options.sessionId = this.apiAiClient.getSessionId();
this.options.lang = this.apiAiClient.getApiLang();
this.options.sessionId = this.apiAiClient.getSessionId();

}
}

public perform(overrideOptions = null): Promise<IServerResponse> {
public perform(overrideOptions = null): Promise<IServerResponse> {

const options = overrideOptions ? overrideOptions : this.options;
const options = overrideOptions ? overrideOptions : this.options;

return XhrRequest.ajax(this.requestMethod, this.uri, options as IStringMap, this.headers)
.then(Request.handleSuccess.bind(this))
.catch(Request.handleError.bind(this));
}
return XhrRequest.ajax(this.requestMethod, this.uri, options as IStringMap, this.headers)
.then(Request.handleSuccess.bind(this))
.catch(Request.handleError.bind(this));
}
}

export default Request;
118 changes: 59 additions & 59 deletions ts/Request/TTSRequest.ts
Expand Up @@ -7,78 +7,78 @@ import Request from "./Request";

export class TTSRequest extends Request {

private static RESPONSE_TYPE_ARRAYBUFFER = "arraybuffer";
private static RESPONSE_TYPE_ARRAYBUFFER = "arraybuffer";

private static audioContext: AudioContext;
private static audioContext: AudioContext;

constructor(protected apiAiClient: ApiAiClient, options: IRequestOptions = {}) {
super(apiAiClient, options);
// this.requestMethod = XhrRequest.Method.GET;
this.uri = ApiAiConstants.DEFAULT_TTS_HOST;
const AudioContext = window.AudioContext || webkitAudioContext;
constructor(protected apiAiClient: ApiAiClient, options: IRequestOptions = {}) {
super(apiAiClient, options);
// this.requestMethod = XhrRequest.Method.GET;
this.uri = ApiAiConstants.DEFAULT_TTS_HOST;
const AudioContext = window.AudioContext || webkitAudioContext;

if (!TTSRequest.audioContext) {
TTSRequest.audioContext = new AudioContext();
}
if (!TTSRequest.audioContext) {
TTSRequest.audioContext = new AudioContext();
}
}

public makeTTSRequest(text: string) {
public makeTTSRequest(text: string) {

if (!text) {
throw new ApiAiClientConfigurationError("Request can not be empty");
}
if (!text) {
throw new ApiAiClientConfigurationError("Request can not be empty");
}

const params = {
lang: "en-US", // <any> this.apiAiClient.getApiLang(),
text: encodeURIComponent(text),
v: this.apiAiClient.getApiVersion()
};
const params = {
lang: "en-US", // <any> this.apiAiClient.getApiLang(),
text: encodeURIComponent(text),
v: this.apiAiClient.getApiVersion()
};

const headers = {
"Accept-language": "en-US",
"Authorization": "Bearer " + this.apiAiClient.getAccessToken()
};
const headers = {
"Accept-language": "en-US",
"Authorization": "Bearer " + this.apiAiClient.getAccessToken()
};

return this.makeRequest(this.uri, params, headers, {responseType: TTSRequest.RESPONSE_TYPE_ARRAYBUFFER})
.then(this.resolveTTSPromise)
.catch(this.rejectTTSPromise.bind(this))
;
}
return this.makeRequest(this.uri, params, headers, {responseType: TTSRequest.RESPONSE_TYPE_ARRAYBUFFER})
.then(this.resolveTTSPromise)
.catch(this.rejectTTSPromise.bind(this))
;
}

private resolveTTSPromise = (data: {response: ArrayBuffer}) => {
return this.speak(data.response);
}
private resolveTTSPromise = (data: {response: ArrayBuffer}) => {
return this.speak(data.response);
}

private rejectTTSPromise = (reason: string) => {
throw new ApiAiRequestError(reason);
}
private rejectTTSPromise = (reason: string) => {
throw new ApiAiRequestError(reason);
}

private makeRequest(url, params, headers, options): Promise<{response: ArrayBuffer}> {
return XhrRequest.get(url, params, headers, options);
}
private makeRequest(url, params, headers, options): Promise<{response: ArrayBuffer}> {
return XhrRequest.get(url, params, headers, options);
}

private speak(data: ArrayBuffer): Promise<{}> {

if (!data.byteLength) {
return Promise.reject("TTS Server unavailable");
}

return new Promise((resolve, reject) => {
TTSRequest.audioContext.decodeAudioData(
data,
(buffer: AudioBuffer) => {
return this.playSound(buffer, resolve);
},
reject
).then(null, (err) => reject(err));
});
private speak(data: ArrayBuffer): Promise<any> {

if (!data.byteLength) {
return Promise.reject("TTS Server unavailable");
}

private playSound(buffer: AudioBuffer, resolve) {
const source = TTSRequest.audioContext.createBufferSource();
source.buffer = buffer;
source.connect(TTSRequest.audioContext.destination);
source.onended = resolve;
source.start(0);
};
return new Promise((resolve, reject) => {
TTSRequest.audioContext.decodeAudioData(
data,
(buffer: AudioBuffer) => {
return this.playSound(buffer, resolve);
},
reject
).then(null, (err) => reject(err));
});
}

private playSound(buffer: AudioBuffer, resolve) {
const source = TTSRequest.audioContext.createBufferSource();
source.buffer = buffer;
source.connect(TTSRequest.audioContext.destination);
source.onended = resolve;
source.start(0);
};
}

0 comments on commit c9eaba6

Please sign in to comment.