Skip to content

Commit

Permalink
dnn-sxc-angular improvements api, query, sxc -> sxcApp, added utils
Browse files Browse the repository at this point in the history
  • Loading branch information
maaaximum-at-2sic committed Mar 11, 2022
1 parent 3e9cd24 commit ae9bb03
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Sxc } from './sxc/sxc-data';
import { SxcApp } from './sxc/sxc-app';
import { NgModule } from '@angular/core';
import { SxcToolbarDirective } from './beta/edit';
// important: for funny reasons this must really got to the real path - if you get it from '.' index it will fail building
Expand All @@ -18,7 +18,7 @@ import { SxcTagToolbarDirective } from './toolbar/tag-toolbar';
SxcTagToolbarDirective,
],
providers: [
Sxc,
SxcApp,
],
exports: [
SxcToolbarDirective,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from './context/context.service';
import { Sxc } from './sxc/sxc-data';
import { SxcApp } from './sxc/sxc-app';
import { DnnInterceptor } from './http/dnn.interceptor';
import { NgModule, Optional, SkipSelf } from '@angular/core';

Expand All @@ -17,7 +17,7 @@ import { NgModule, Optional, SkipSelf } from '@angular/core';
// HttpClientModule,
],
providers: [
Sxc,
SxcApp,
Context,
DnnInterceptor,
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { getHttpParams } from '../../utils/params';
import { routeApi } from '../contants';

/**
Expand All @@ -24,28 +25,32 @@ export class Api {
/**
* Do a GET request to the specified 2sxc api controller
*/
get<T>(method: string, params?: HttpParams): Observable<T> {
return this.http.get<T>(this.url(method), { params });
get<T>(method: string, params: HttpParams | string): Observable<T> {
const requestParams: HttpParams = getHttpParams(params);
return this.http.get<T>(this.url(method), { params: requestParams });
}

/**
* Do a POST request to the specified 2sxc api controller
*/
post<T>(method: string, body: T, params?: HttpParams): Observable<T> {
return this.http.post<T>(this.url(method), body, { params });
post<T>(method: string, params: HttpParams | string, body: T): Observable<T> {
const requestParams: HttpParams = getHttpParams(params);
return this.http.post<T>(this.url(method), body, { params: requestParams });
}

/**
* Do a PUT request to the specified 2sxc api controller
*/
put<T>(method: string, body: T, params?: HttpParams): Observable<T> {
return this.http.put<T>(this.url(method), body, { params });
put<T>(methodAndParams: string, params: HttpParams | string, body: T): Observable<T> {
const requestParams: HttpParams = getHttpParams(params);
return this.http.put<T>(this.url(methodAndParams), body, { params: requestParams });
}

/**
* Do a DELETE request to the specified 2sxc api controller
*/
delete<T>(method: string, params?: HttpParams): Observable<T> {
return this.http.put<T>(this.url(method), { params });
delete<T>(method: string, params: HttpParams | string): Observable<T> {
const requestParams: HttpParams = getHttpParams(params);
return this.http.put<T>(this.url(method), { params: requestParams });
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './api';
export * from './data';
export * from './sxc-data';
export * from './sxc-app';
export * from './query';
export * from './metadata-for';
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { getHttpParams } from '../../utils/params';
import { routeQuery } from '../contants';

/**
Expand Down Expand Up @@ -39,11 +40,9 @@ export class Query<T> {
*/
getAll(params?: HttpParams | string, data?: T): Observable<T> {
const url = `${routeQuery}/${this.name}`;
let streamParams: HttpParams = null;
const streamParams: HttpParams = getHttpParams(params);

if (params) streamParams = new HttpParams({fromString: params.toString() });
if (data) return this.http.post<T>(url, data, { params: streamParams });

return this.http.get<T>(url, { params: streamParams });
}

Expand All @@ -69,11 +68,9 @@ export class Query<T> {
*/
getStream(stream: string, params?: HttpParams | string, data?: T): Observable<T> {
const url = `${routeQuery}/${this.name}?${this.streamParamKey}=${stream}`;
let streamParams: HttpParams = null;
const streamParams: HttpParams = getHttpParams(params);

if (params) streamParams = new HttpParams({fromString: params.toString() });
if (data) return this.http.post<T>(url, data, { params: streamParams }).pipe(map(res => res[stream]));

return this.http.get<T>(url, { params: streamParams }).pipe(map(res => res[stream]));
}

Expand All @@ -99,11 +96,9 @@ export class Query<T> {
*/
getStreams(streams: string[], params?: HttpParams | string, data?: T): Observable<T> {
const url = `${routeQuery}/${this.name}?${this.streamParamKey}=${streams.join(',')}`;
let streamParams: HttpParams = null;
const streamParams: HttpParams = getHttpParams(params);

if (params) streamParams = new HttpParams({fromString: params.toString() });
if (data) return this.http.post<T>(url, data, { params: streamParams });

return this.http.get<T>(url, { params: streamParams });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Api } from './api';
import { Data } from './data';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Query } from './query';

/**
* 2sxc data provider
* gives you access to content and query streams using the content$ and query$ commands
* you can also use the content and query managers, but these are currently not so useful.
*
* @export
* @class SxcData
*/
@Injectable({
providedIn: 'root',
})
export class SxcApp {
constructor(
private http: HttpClient,
) { }

/**
* Cet a content manager object for a specific ContentType
* @param contentType name of the content-type
* @returns a query object with .getAll(), .getOne(), .create(), .update(), .delete()
*/
public data<T>(contentType: string): Data<T> {
return new Data<T>(this.http, contentType);
}

/**
* get a query object to then start queries
* @param name the query name
* @returns a query object with .getAll(), .getStreams(), .getStream()
*/
public query<T>(name: string) {
return new Query<T>(this.http, name);
}

/**
* get an api object to then start api-calls
* @param controller the api controller
* @returns an API object with .url(), .get<T>(), .post<T>(), .put<T>(), .delete<T>() method
*/
public api(controller: string): Api {
return new Api(this.http, controller);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { HttpParams } from '@angular/common/http';

export function getHttpParams(params: HttpParams | string) {
return typeof(params) !== 'string' ? params : new HttpParams({fromString: params});
}

0 comments on commit ae9bb03

Please sign in to comment.