Commit 1a0b4651 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Support des options pour les requêtes.

parent 9fcae790
......@@ -39,18 +39,25 @@ class Parameter implements \IteratorAggregate, \JsonSerializable
*/
private $optional = false;
/**
* @var string
*/
private $default;
/**
* Parameter constructor.
*
* @param string $name
* @param Type $type
* @param bool $optional
* @param string $name
* @param Type $type
* @param bool $optional
* @param string|null $default
*/
public function __construct(string $name, Type $type, bool $optional = false)
public function __construct(string $name, Type $type, bool $optional = false, ?string $default = null)
{
$this->name = $name;
$this->type = $type;
$this->optional = $optional;
$this->default = $default;
}
/**
......@@ -68,7 +75,18 @@ class Parameter implements \IteratorAggregate, \JsonSerializable
*/
public function getDeclaration(): string
{
return sprintf('%s%s: %s', $this->name, $this->optional ? '?' : '', $this->type->getUsage());
$parts = [$this->name];
if ($this->optional) {
$parts[] = '?';
}
$parts[] = ': ';
$parts[] = $this->type->getUsage();
if ($this->default !== null) {
$parts[] = ' = ';
$parts[] = $this->default;
}
return implode('', $parts);
}
/**
......
......@@ -94,7 +94,7 @@ final class OperationMapper
}
}
$opParameters = $options = $body = [];
$opParameters = $body = [];
$callParameters = [TypescriptHelper::quoteString($this->operation->getMethod())];
$callPath = $path = $this->pathParser->parse($this->operation->getPath(), $this->operation->getRequirements());
......@@ -128,20 +128,18 @@ final class OperationMapper
}
$opParameters[] = new Parameter('body', $requestBody);
$options[] = 'body';
$body[] = 'options.body = body;';
}
$filterProperties = $this->getFilterProperties();
if ($filterProperties) {
[$filterBody, $filterParam] = $this->buildFilterBody($filterProperties, '_params');
[$filterBody, $filterParam] = $this->buildFilterBody($filterProperties, 'options.params');
$body = array_merge($body, $filterBody);
$opParameters[] = $filterParam;
$options[] = 'params: _params';
}
if ($options) {
$callParameters[] = '{ ' . implode(', ', $options) . ' }';
}
$opParameters[] = new Parameter('options', Placeholder::get('RequestOptions'), false, '{}');
$callParameters[] = 'options';
$returnType = $responseBody ?: Placeholder::get('HttpResponseBase');
......@@ -284,18 +282,20 @@ final class OperationMapper
);
$this->typeFactory->add($filterName, $filterType);
$body = ["const $varName: { [name: string]: string | string[] } = {};"];
$body[] = "if (params && typeof params === 'object') {";
$body[] = "if (filters && typeof filters === 'object') {";
$body[] = " if (!$varName) {";
$body[] = " $varName = {};";
$body[] = ' }';
foreach ($filterProperties as $property) {
$assignation = sprintf(
' %s = %s;',
$property->getUsage($varName, true),
$property->getType()->castToStringOrStringArray($property->getUsage('params'))
$property->getType()->castToStringOrStringArray($property->getUsage('filters'))
);
if ($property->isNullable()) {
$body[] = sprintf(
" if (%s) {\n %s\n }",
TypescriptHelper::propertyTestor('params', $property->getName()),
TypescriptHelper::propertyTestor('filters', $property->getName()),
$assignation
);
} else {
......@@ -304,6 +304,6 @@ final class OperationMapper
}
$body[] = '}';
return [$body, new Parameter('params', $filterType, true)];
return [$body, new Parameter('filters', $filterType, true)];
}
}
......@@ -83,12 +83,22 @@ export type UUID = string;
export function isUUID(data: any): data is UUID {
return typeof data === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu.test(data);
}
/**
* Full DateTime in ISO-8601 format.
*/
export type DateTime = string;
/**
* Options supplémentaires pour les requêtes.
*/
export interface RequestOptions {
body?: any;
headers?: HttpHeaders | {
[header: string]: string | string[];
};
params?: { [param: string]: string | string[]; };
}
/**
* AbstractResourceCache est une classe abstraite qui définit l'interface d'un cache de ressources.
*
......@@ -339,13 +349,13 @@ export interface APIService<API extends APIMeta> {
/**
* Récupère une ressource par son IRI ou par son type et les paramètres de son IRI.
*/
get<R extends Resource>(iri: IRI<R>): Observable<R> ;
get<T extends keyof API>(type: T, parameters: API[T]['iriParameters']): Observable<API[T]['resource']> ;
get<R extends Resource>(iri: IRI<R>, options?: RequestOptions): Observable<R> ;
get<T extends keyof API>(type: T, parameters: API[T]['iriParameters'], options?: RequestOptions): Observable<API[T]['resource']> ;
/**
* Récupère des ressources par leurs IRIs.
*/
getMany<R extends Resource>(iris: IRI<R>[]): Observable<R[]>;
getMany<R extends Resource>(iris: IRI<R>[], options?: RequestOptions): Observable<R[]>;
/**
* Génère l'IRI d'une resource à partir de son type et des paramètres d'IRI.
......@@ -373,12 +383,12 @@ export abstract class AbstractAPIService<
private readonly client: HttpClient
) {}
public get<R extends Resource>(iri: IRI<R>): Observable<R>;
public get<T extends keyof API>(type: T, parameters: API[T]['iriParameters']): Observable<API[T]['resource']>;
public get<R extends Resource>(iri: IRI<R>, options?: RequestOptions): Observable<R>;
public get<T extends keyof API>(type: T, parameters: API[T]['iriParameters'], options?: RequestOptions): Observable<API[T]['resource']>;
public get<T extends keyof API, R extends Resource = API[T]['resource']>(
typeOrIRI: T | IRI<R>,
parameters?: API[T]['iriParameters']
parameters?: API[T]['iriParameters'], options?: RequestOptions
): Observable<R> {
let iri: IRI<R>;
if (this.metadata.has(typeOrIRI as string)) {
......@@ -386,11 +396,11 @@ export abstract class AbstractAPIService<
} else {
iri = typeOrIRI as IRI<R>;
}
return this.cache.get(iri, () => this.client.get<R>(iri as any));
return this.cache.get(iri, () => this.client.get<R>(iri as any, options));
}
public getMany<R extends Resource>(iris: IRI<R>[]): Observable<R[]> {
return forkJoin(iris.map(iri => this.get(iri)));
public getMany<R extends Resource>(iris: IRI<R>[], options?: RequestOptions): Observable<R[]> {
return forkJoin(iris.map(iri => this.get(iri, options)));
}
public generateIRI<T extends keyof API>(type: T, parameters: API[T]['iriParameters']): IRI<API[T]['resource']> {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment