Skip to content

Commit

Permalink
update markup client
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Sep 24, 2022
1 parent 6da56a4 commit 0bb960a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 13 deletions.
40 changes: 33 additions & 7 deletions src/Generator/Markup/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
use PSX\Api\Generator\Client\Dto;
use PSX\Api\GeneratorInterface;
use PSX\Api\SpecificationInterface;
use PSX\Schema\DefinitionsInterface;
use PSX\Schema\Generator\TypeScript;
use PSX\Schema\GeneratorInterface as SchemaGeneratorInterface;
use PSX\Schema\Schema;
use PSX\Schema\TypeFactory;

/**
* Client
Expand All @@ -53,22 +56,45 @@ public function generate(SpecificationInterface $specification): string
$client = $this->converter->getClient($specification);

$lines = [];
$lines[] = 'var client = new ' . $client->className . '(...)';
$lines[] = 'const client = new ' . $client->className . '(...)';

foreach ($client->resources as $resource) {
/** @var Dto\Resource $resource */
foreach ($resource->methods as $methodName => $method) {
/** @var Dto\Resource $method */
/** @var Dto\Method $method */

$resourceArguments = implode(', ', array_keys($resource->properties));
$methodArguments = implode(', ', array_keys(iterator_to_array($method->args ?? [])));
$resourceArguments = implode(', ', array_keys($resource->properties ?? []));
$arguments = implode(', ', $this->getArguments($method));
$returnType = isset($method->return) ? $method->return->type : 'void';

$lines[] = 'client.' . $resource->methodName . '(' . $resourceArguments . ').' . $methodName . '(' . $methodArguments . ')';
$lines[] = 'client.' . $resource->methodName . '(' . $resourceArguments . ').' . $methodName . '(' . $arguments . '): ' . $returnType;
}

$lines[] = "\n";
}

$lines[] = "";
$lines[] = $this->generateSchema($specification->getDefinitions());

return implode("\n", $lines);
}

private function getArguments(Dto\Method $method): array
{
$arguments = [];
foreach ($method->args as $type) {
$arguments[] = $type->type;
}

return $arguments;
}

private function generateSchema(DefinitionsInterface $definitions): string
{
$schema = new Schema(TypeFactory::getAny(), $definitions);
$return = $this->generator->generate($schema);
$return = str_replace('export interface', 'interface', $return);
$return = preg_replace('/^import(.*);$/ims', '', $return);
$return = str_replace("\n\n\n", "\n\n", $return);

return $return;
}
}
68 changes: 62 additions & 6 deletions tests/Generator/Markup/resource/client.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
var client = new Client(...)
client.getFooByNameAndType(name, type).listFoo(query)
client.getFooByNameAndType(name, type).createFoo(data)
client.getFooByNameAndType(name, type).put(data)
client.getFooByNameAndType(name, type).delete()
client.getFooByNameAndType(name, type).patch(data)
const client = new Client(...)
client.getFooByNameAndType(name, type).listFoo(GetQuery): EntryCollection
client.getFooByNameAndType(name, type).createFoo(EntryCreate): EntryMessage
client.getFooByNameAndType(name, type).put(EntryUpdate): EntryMessage
client.getFooByNameAndType(name, type).delete(): EntryMessage
client.getFooByNameAndType(name, type).patch(EntryPatch): EntryMessage

interface Path {
name: string
type?: string
}

interface GetQuery {
startIndex: number
float?: number
boolean?: boolean
date?: string
datetime?: string
}

interface EntryCollection {
entry?: Array<Entry>
}

interface Entry {
id?: number
userId?: number
title?: string
date?: string
}

interface EntryCreate {
id?: number
userId?: number
title: string
date: string
}

interface EntryMessage {
success?: boolean
message?: string
}

interface EntryUpdate {
id: number
userId?: number
title?: string
date?: string
}

interface EntryDelete {
id: number
userId?: number
title?: string
date?: string
}

interface EntryPatch {
id: number
userId?: number
title?: string
date?: string
}

0 comments on commit 0bb960a

Please sign in to comment.