@microsoft/paris
Advanced tools
Comparing version 1.3.8 to 1.4.0
@@ -5,3 +5,4 @@ { | ||
"todolist" | ||
] | ||
], | ||
"typescript.tsdk": "node_modules\\typescript\\lib" | ||
} |
@@ -133,3 +133,3 @@ var __extends = (this && this.__extends) || (function () { | ||
var itemCreators = itemsData.map(function (savedItemData) { return _this.createItem(savedItemData); }); | ||
return combineLatest.apply(_this, itemCreators); | ||
return combineLatest(itemCreators); | ||
})); | ||
@@ -136,0 +136,0 @@ }; |
@@ -5,3 +5,3 @@ import { EntityConfigFunctionOrValue } from "./entity.config"; | ||
import { DataQuery } from "../data_access/data-query"; | ||
export interface ApiCallBackendConfigInterface<T = any, TRawData = any> { | ||
export interface ApiCallBackendConfigInterface<T = any, TRawData = any, TInput = any> { | ||
/** | ||
@@ -56,2 +56,10 @@ * The URL to use for HTTP requests. | ||
parseData?: (data: TRawData, config?: ParisConfig, query?: DataQuery) => T; | ||
/** | ||
* Custom headers for API call. | ||
* It can be either a dictionary of string, with header names as the keys, | ||
* or a function (which be applied by Paris) which receives data and config, and returns the headers for the API call. | ||
* @param {TInput} data | ||
* @param {ParisConfig} config | ||
*/ | ||
customHeaders?: ((data: TInput, config?: ParisConfig) => Record<string, string>) | Record<string, string>; | ||
} |
@@ -21,2 +21,3 @@ import { Observable } from "rxjs"; | ||
data?: T; | ||
customHeaders?: Record<string, string>; | ||
params?: U; | ||
@@ -23,0 +24,0 @@ separateArrayParams?: boolean; |
@@ -0,1 +1,9 @@ | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
import { ajax } from "rxjs/ajax"; | ||
@@ -57,2 +65,7 @@ import { catchError, map } from "rxjs/operators"; | ||
requestOptions.body = options.data; | ||
//handle custom headers | ||
if (options && options.customHeaders) { | ||
var headers = __assign({}, options.customHeaders, requestOptions.headers); | ||
return __assign({}, requestOptions, { headers: headers }); | ||
} | ||
return requestOptions; | ||
@@ -59,0 +72,0 @@ }; |
@@ -123,2 +123,8 @@ var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
: null; | ||
if (!httpOptions) { | ||
httpOptions = {}; | ||
} | ||
if (apiCallType.config.customHeaders) { | ||
httpOptions.customHeaders = apiCallType.config.customHeaders instanceof Function ? apiCallType.config.customHeaders(input, this.config) : apiCallType.config.customHeaders; | ||
} | ||
var requestOptions = apiCallType.config.responseType ? { responseType: apiCallType.config.responseType } : null; | ||
@@ -251,2 +257,8 @@ var apiCall$ = this.makeApiCall(apiCallType.config, apiCallType.config.method || "GET", httpOptions, undefined, requestOptions) | ||
var httpOptions = backendConfig.parseDataQuery ? { params: backendConfig.parseDataQuery(query) } : queryToHttpOptions(query); | ||
if (!httpOptions) { | ||
httpOptions = {}; | ||
} | ||
if (backendConfig.customHeaders) { | ||
httpOptions.customHeaders = backendConfig.customHeaders instanceof Function ? backendConfig.customHeaders(query, this.config) : backendConfig.customHeaders; | ||
} | ||
var endpoint = backendConfig.endpoint instanceof Function ? backendConfig.endpoint(this.config, query) : backendConfig.endpoint; | ||
@@ -253,0 +265,0 @@ var apiCallConfig = Object.assign({}, backendConfig, { |
{ | ||
"name": "@microsoft/paris", | ||
"version": "1.3.8", | ||
"version": "1.4.0", | ||
"description": "Library for the implementation of Domain Driven Design with TypeScript + RxJS", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -44,3 +44,3 @@ # Paris | ||
import { Entity, EntityModelBase } from "@microsoft/paris"; | ||
import { Entity, EntityModelBase, EntityField } from "@microsoft/paris"; | ||
@@ -47,0 +47,0 @@ @Entity({ |
@@ -48,3 +48,3 @@ import {Paris} from '../../lib/paris'; | ||
errorCallback = jest.fn(); | ||
DataStoreService.prototype.save = jest.fn(() => of(newTodoItem)); | ||
DataStoreService.prototype.save = jest.fn(() => of(newTodoItem) as any); | ||
}); | ||
@@ -51,0 +51,0 @@ |
@@ -9,5 +9,6 @@ import {ApiCall} from "../../lib/config/decorators/api-call.decorator"; | ||
method: "POST", | ||
cache: true | ||
cache: true, | ||
customHeaders: ((data, config) => config.entityIdProperty === 'id' && data === "test" ? ({"testHeader": "testValue"}) : undefined) | ||
}) | ||
export class CreateTodoListApiCall extends ApiCallModel<Array<Todo>, string>{ | ||
} |
@@ -217,6 +217,6 @@ import {Observable, of} from 'rxjs'; | ||
DataStoreService.prototype.get = jest.fn((endpoint: string) => createData(endpoint)); | ||
DataStoreService.prototype.get = jest.fn((endpoint: string) => createData(endpoint)) as any; | ||
DataStoreService.prototype.request = jest.fn((method: string, endpoint: string) => | ||
createData(endpoint) | ||
); | ||
) as any; | ||
}); | ||
@@ -223,0 +223,0 @@ |
@@ -72,12 +72,12 @@ import { Observable, of } from 'rxjs'; | ||
it('should call Repository.getItemById with correct params', () => { | ||
paris.getItemById(Todo, 1, null, { test: 1 }); | ||
expect(repo.getItemById).toHaveBeenCalledWith(1, defaultDataOptions, { test: 1 }); | ||
paris.getItemById(Todo, 1, null, { test: 1 , customHeaders: {'TestHeader': 'TestValue'}}); | ||
expect(repo.getItemById).toHaveBeenCalledWith(1, defaultDataOptions, { test: 1, customHeaders: {'TestHeader': 'TestValue'} }); | ||
}); | ||
it('should call Http.request with correct params', () => { | ||
paris.getItemById(Todo, 1, null, { test: 1 }); | ||
paris.getItemById(Todo, 1, null, { test: 1, customHeaders: {'TestHeader': 'TestValue'}}); | ||
expect(paris.dataStore.httpService.request).toHaveBeenCalledWith( | ||
'GET', | ||
'/todo/1', | ||
{ params: { test: 1 } }, | ||
{ params: { test: 1, customHeaders: {'TestHeader': 'TestValue'} }}, | ||
{ timeout: 20000 } | ||
@@ -167,4 +167,4 @@ ); | ||
describe('apiCall', () => { | ||
let jestGetApiCallCacheSpy: jest.SpyInstance<Paris>; | ||
let jestMakeApiCallSpy: jest.SpyInstance<Paris>; | ||
let jestGetApiCallCacheSpy: jest.SpyInstance<Observable<Paris>>; | ||
let jestMakeApiCallSpy: jest.SpyInstance<Observable<Paris>>; | ||
@@ -185,5 +185,4 @@ beforeEach(() => { | ||
jestGetApiCallCacheSpy.mockRestore(); | ||
const fakeCache = { get: () => of(null) }; | ||
jest.spyOn(fakeCache, 'get'); | ||
paris['getApiCallCache'] = jest.fn(() => fakeCache); | ||
const fakeCache = { get: jest.fn(() => of(null)) }; | ||
paris['getApiCallCache'] = jest.fn(() => fakeCache) as any; | ||
@@ -202,3 +201,3 @@ paris.apiCall(CreateTodoListApiCall); | ||
it('should be able to serialize complex objects with circular dependencies', done => { | ||
jestMakeApiCallSpy.mockReturnValue(of(null)); | ||
jestMakeApiCallSpy.mockReturnValue(of(new Paris)); | ||
@@ -249,2 +248,28 @@ | ||
it('should call makeApiCall with the right custom headers which are given by a callback', () => { | ||
paris.apiCall(CreateTodoListApiCall, "test", { allowCache: false }); | ||
expect((<any>paris).makeApiCall).toHaveBeenCalledWith( | ||
{"cache": true, "customHeaders": jasmine.any(Function), "endpoint": "create_new_list", "method": "POST", "name": "Create a new Todo list"}, | ||
'POST', | ||
{"customHeaders": {"testHeader": "testValue"}, "data": "test"}, | ||
undefined, | ||
null | ||
); | ||
}); | ||
it('should call makeApiCall with the right custom headers which are given directly', () => { | ||
const createToDoListApiCall = Object.assign({}, CreateTodoListApiCall, {config: Object.assign({}, (<any>CreateTodoListApiCall).config, {'customHeaders': {'directTestHeader': 'directTestValue'}})}); | ||
paris.apiCall(createToDoListApiCall, undefined, { allowCache: false }); | ||
expect((<any>paris).makeApiCall).toHaveBeenCalled(); | ||
expect((<any>paris).makeApiCall).toHaveBeenCalledWith( | ||
{"cache": true, "customHeaders": {"directTestHeader": "directTestValue"}, "endpoint": "create_new_list", "method": "POST", "name": "Create a new Todo list"}, | ||
'POST', | ||
{"customHeaders": {"directTestHeader": "directTestValue"}}, | ||
undefined, | ||
null | ||
); | ||
}); | ||
it('should call Http.request with correct default params', () => { }); | ||
@@ -251,0 +276,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
247738
5045