Replace submodules with full folder contents
This commit is contained in:
1
fuse-starter-v20.0.0/src/@fuse/lib/mock-api/index.ts
Normal file
1
fuse-starter-v20.0.0/src/@fuse/lib/mock-api/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from '@fuse/lib/mock-api/public-api';
|
||||
@ -0,0 +1,5 @@
|
||||
import { InjectionToken } from '@angular/core';
|
||||
|
||||
export const FUSE_MOCK_API_DEFAULT_DELAY = new InjectionToken<number>(
|
||||
'FUSE_MOCK_API_DEFAULT_DELAY'
|
||||
);
|
||||
@ -0,0 +1,82 @@
|
||||
import {
|
||||
HttpErrorResponse,
|
||||
HttpEvent,
|
||||
HttpHandlerFn,
|
||||
HttpRequest,
|
||||
HttpResponse,
|
||||
} from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import { FUSE_MOCK_API_DEFAULT_DELAY } from '@fuse/lib/mock-api/mock-api.constants';
|
||||
import { FuseMockApiService } from '@fuse/lib/mock-api/mock-api.service';
|
||||
import { Observable, delay, of, switchMap, throwError } from 'rxjs';
|
||||
|
||||
export const mockApiInterceptor = (
|
||||
request: HttpRequest<unknown>,
|
||||
next: HttpHandlerFn
|
||||
): Observable<HttpEvent<unknown>> => {
|
||||
const defaultDelay = inject(FUSE_MOCK_API_DEFAULT_DELAY);
|
||||
const fuseMockApiService = inject(FuseMockApiService);
|
||||
|
||||
// Try to get the request handler
|
||||
const { handler, urlParams } = fuseMockApiService.findHandler(
|
||||
request.method.toUpperCase(),
|
||||
request.url
|
||||
);
|
||||
|
||||
// Pass through if the request handler does not exist
|
||||
if (!handler) {
|
||||
return next(request);
|
||||
}
|
||||
|
||||
// Set the intercepted request on the handler
|
||||
handler.request = request;
|
||||
|
||||
// Set the url params on the handler
|
||||
handler.urlParams = urlParams;
|
||||
|
||||
// Subscribe to the response function observable
|
||||
return handler.response.pipe(
|
||||
delay(handler.delay ?? defaultDelay ?? 0),
|
||||
switchMap((response) => {
|
||||
// If there is no response data,
|
||||
// throw an error response
|
||||
if (!response) {
|
||||
response = new HttpErrorResponse({
|
||||
error: 'NOT FOUND',
|
||||
status: 404,
|
||||
statusText: 'NOT FOUND',
|
||||
});
|
||||
|
||||
return throwError(response);
|
||||
}
|
||||
|
||||
// Parse the response data
|
||||
const data = {
|
||||
status: response[0],
|
||||
body: response[1],
|
||||
};
|
||||
|
||||
// If the status code is in between 200 and 300,
|
||||
// return a success response
|
||||
if (data.status >= 200 && data.status < 300) {
|
||||
response = new HttpResponse({
|
||||
body: data.body,
|
||||
status: data.status,
|
||||
statusText: 'OK',
|
||||
});
|
||||
|
||||
return of(response);
|
||||
}
|
||||
|
||||
// For other status codes,
|
||||
// throw an error response
|
||||
response = new HttpErrorResponse({
|
||||
error: data.body.error,
|
||||
status: data.status,
|
||||
statusText: 'ERROR',
|
||||
});
|
||||
|
||||
return throwError(response);
|
||||
})
|
||||
);
|
||||
};
|
||||
@ -0,0 +1,87 @@
|
||||
import { HttpRequest } from '@angular/common/http';
|
||||
import { FuseMockApiReplyCallback } from '@fuse/lib/mock-api/mock-api.types';
|
||||
import { Observable, of, take, throwError } from 'rxjs';
|
||||
|
||||
export class FuseMockApiHandler {
|
||||
request!: HttpRequest<any>;
|
||||
urlParams!: { [key: string]: string };
|
||||
|
||||
// Private
|
||||
private _reply: FuseMockApiReplyCallback = undefined;
|
||||
private _replyCount = 0;
|
||||
private _replied = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
constructor(
|
||||
public url: string,
|
||||
public delay?: number
|
||||
) {}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Accessors
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Getter for response callback
|
||||
*/
|
||||
get response(): Observable<any> {
|
||||
// If the execution limit has been reached, throw an error
|
||||
if (this._replyCount > 0 && this._replyCount <= this._replied) {
|
||||
return throwError('Execution limit has been reached!');
|
||||
}
|
||||
|
||||
// If the response callback has not been set, throw an error
|
||||
if (!this._reply) {
|
||||
return throwError('Response callback function does not exist!');
|
||||
}
|
||||
|
||||
// If the request has not been set, throw an error
|
||||
if (!this.request) {
|
||||
return throwError('Request does not exist!');
|
||||
}
|
||||
|
||||
// Increase the replied count
|
||||
this._replied++;
|
||||
|
||||
// Execute the reply callback
|
||||
const replyResult = this._reply({
|
||||
request: this.request,
|
||||
urlParams: this.urlParams,
|
||||
});
|
||||
|
||||
// If the result of the reply callback is an observable...
|
||||
if (replyResult instanceof Observable) {
|
||||
// Return the result as it is
|
||||
return replyResult.pipe(take(1));
|
||||
}
|
||||
|
||||
// Otherwise, return the result as an observable
|
||||
return of(replyResult).pipe(take(1));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Public methods
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Reply
|
||||
*
|
||||
* @param callback
|
||||
*/
|
||||
reply(callback: FuseMockApiReplyCallback): void {
|
||||
// Store the reply
|
||||
this._reply = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reply count
|
||||
*
|
||||
* @param count
|
||||
*/
|
||||
replyCount(count: number): void {
|
||||
// Store the reply count
|
||||
this._replyCount = count;
|
||||
}
|
||||
}
|
||||
201
fuse-starter-v20.0.0/src/@fuse/lib/mock-api/mock-api.service.ts
Normal file
201
fuse-starter-v20.0.0/src/@fuse/lib/mock-api/mock-api.service.ts
Normal file
@ -0,0 +1,201 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { FuseMockApiHandler } from '@fuse/lib/mock-api/mock-api.request-handler';
|
||||
import { FuseMockApiMethods } from '@fuse/lib/mock-api/mock-api.types';
|
||||
import { compact, fromPairs } from 'lodash-es';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class FuseMockApiService {
|
||||
private _handlers: { [key: string]: Map<string, FuseMockApiHandler> } = {
|
||||
get: new Map<string, FuseMockApiHandler>(),
|
||||
post: new Map<string, FuseMockApiHandler>(),
|
||||
patch: new Map<string, FuseMockApiHandler>(),
|
||||
delete: new Map<string, FuseMockApiHandler>(),
|
||||
put: new Map<string, FuseMockApiHandler>(),
|
||||
head: new Map<string, FuseMockApiHandler>(),
|
||||
jsonp: new Map<string, FuseMockApiHandler>(),
|
||||
options: new Map<string, FuseMockApiHandler>(),
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Public methods
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Find the handler from the service
|
||||
* with the given method and url
|
||||
*
|
||||
* @param method
|
||||
* @param url
|
||||
*/
|
||||
findHandler(
|
||||
method: string,
|
||||
url: string
|
||||
): {
|
||||
handler: FuseMockApiHandler | undefined;
|
||||
urlParams: { [key: string]: string };
|
||||
} {
|
||||
// Prepare the return object
|
||||
const matchingHandler: {
|
||||
handler: FuseMockApiHandler | undefined;
|
||||
urlParams: { [key: string]: string };
|
||||
} = {
|
||||
handler: undefined,
|
||||
urlParams: {},
|
||||
};
|
||||
|
||||
// Split the url
|
||||
const urlParts = url.split('/');
|
||||
|
||||
// Get all related request handlers
|
||||
const handlers = this._handlers[method.toLowerCase()];
|
||||
|
||||
// Iterate through the handlers
|
||||
handlers.forEach((handler, handlerUrl) => {
|
||||
// Skip if there is already a matching handler
|
||||
if (matchingHandler.handler) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Split the handler url
|
||||
const handlerUrlParts = handlerUrl.split('/');
|
||||
|
||||
// Skip if the lengths of the urls we are comparing are not the same
|
||||
if (urlParts.length !== handlerUrlParts.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Compare
|
||||
const matches = handlerUrlParts.every(
|
||||
(handlerUrlPart, index) =>
|
||||
handlerUrlPart === urlParts[index] ||
|
||||
handlerUrlPart.startsWith(':')
|
||||
);
|
||||
|
||||
// If there is a match...
|
||||
if (matches) {
|
||||
// Assign the matching handler
|
||||
matchingHandler.handler = handler;
|
||||
|
||||
// Extract and assign the parameters
|
||||
matchingHandler.urlParams = fromPairs(
|
||||
compact(
|
||||
handlerUrlParts.map((handlerUrlPart, index) =>
|
||||
handlerUrlPart.startsWith(':')
|
||||
? [handlerUrlPart.substring(1), urlParts[index]]
|
||||
: undefined
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return matchingHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register GET request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onGet(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('get', url, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register POST request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onPost(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('post', url, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register PATCH request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onPatch(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('patch', url, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register DELETE request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onDelete(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('delete', url, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register PUT request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onPut(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('put', url, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register HEAD request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onHead(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('head', url, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register JSONP request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onJsonp(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('jsonp', url, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register OPTIONS request handler
|
||||
*
|
||||
* @param url - URL address of the mocked API endpoint
|
||||
* @param delay - Delay of the response in milliseconds
|
||||
*/
|
||||
onOptions(url: string, delay?: number): FuseMockApiHandler {
|
||||
return this._registerHandler('options', url, delay);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Private methods
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Register and return a new instance of the handler
|
||||
*
|
||||
* @param method
|
||||
* @param url
|
||||
* @param delay
|
||||
* @private
|
||||
*/
|
||||
private _registerHandler(
|
||||
method: FuseMockApiMethods,
|
||||
url: string,
|
||||
delay?: number
|
||||
): FuseMockApiHandler {
|
||||
// Create a new instance of FuseMockApiRequestHandler
|
||||
const fuseMockHttp = new FuseMockApiHandler(url, delay);
|
||||
|
||||
// Store the handler to access it from the interceptor
|
||||
this._handlers[method].set(url, fuseMockHttp);
|
||||
|
||||
// Return the instance
|
||||
return fuseMockHttp;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import { HttpRequest } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
export type FuseMockApiReplyCallback =
|
||||
| ((data: {
|
||||
request: HttpRequest<any>;
|
||||
urlParams: { [key: string]: string };
|
||||
}) => [number, string | any] | Observable<any>)
|
||||
| undefined;
|
||||
|
||||
export type FuseMockApiMethods =
|
||||
| 'get'
|
||||
| 'post'
|
||||
| 'patch'
|
||||
| 'delete'
|
||||
| 'put'
|
||||
| 'head'
|
||||
| 'jsonp'
|
||||
| 'options';
|
||||
@ -0,0 +1,30 @@
|
||||
export class FuseMockApiUtils {
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Public methods
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generate a globally unique id
|
||||
*/
|
||||
static guid(): string {
|
||||
/* eslint-disable */
|
||||
|
||||
let d = new Date().getTime();
|
||||
|
||||
// Use high-precision timer if available
|
||||
if (
|
||||
typeof performance !== 'undefined' &&
|
||||
typeof performance.now === 'function'
|
||||
) {
|
||||
d += performance.now();
|
||||
}
|
||||
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
const r = (d + Math.random() * 16) % 16 | 0;
|
||||
d = Math.floor(d / 16);
|
||||
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
|
||||
});
|
||||
|
||||
/* eslint-enable */
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
export * from '@fuse/lib/mock-api/mock-api.constants';
|
||||
export * from '@fuse/lib/mock-api/mock-api.interceptor';
|
||||
export * from '@fuse/lib/mock-api/mock-api.service';
|
||||
export * from '@fuse/lib/mock-api/mock-api.types';
|
||||
export * from '@fuse/lib/mock-api/mock-api.utils';
|
||||
Reference in New Issue
Block a user