|
@@ -53,7 +53,7 @@ class PostmanCollectionWriter
|
|
'name' => config('scribe.title') ?: config('app.name') . ' API',
|
|
'name' => config('scribe.title') ?: config('app.name') . ' API',
|
|
'_postman_id' => Uuid::uuid4()->toString(),
|
|
'_postman_id' => Uuid::uuid4()->toString(),
|
|
'description' => $description,
|
|
'description' => $description,
|
|
- 'schema' => "https://schema.getpostman.com/json/collection/v".self::VERSION."/collection.json",
|
|
|
|
|
|
+ 'schema' => "https://schema.getpostman.com/json/collection/v" . self::VERSION . "/collection.json",
|
|
],
|
|
],
|
|
'item' => $groupedEndpoints->map(function (Collection $routes, $groupName) {
|
|
'item' => $groupedEndpoints->map(function (Collection $routes, $groupName) {
|
|
return [
|
|
return [
|
|
@@ -62,15 +62,52 @@ class PostmanCollectionWriter
|
|
'item' => $routes->map(\Closure::fromCallable([$this, 'generateEndpointItem']))->toArray(),
|
|
'item' => $routes->map(\Closure::fromCallable([$this, 'generateEndpointItem']))->toArray(),
|
|
];
|
|
];
|
|
})->values()->toArray(),
|
|
})->values()->toArray(),
|
|
|
|
+ 'auth' => $this->generateAuthObject(),
|
|
];
|
|
];
|
|
|
|
|
|
- if (! empty($this->auth)) {
|
|
|
|
|
|
+ if (!empty($this->auth)) {
|
|
$collection['auth'] = $this->auth;
|
|
$collection['auth'] = $this->auth;
|
|
}
|
|
}
|
|
|
|
|
|
return $collection;
|
|
return $collection;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ protected function generateAuthObject()
|
|
|
|
+ {
|
|
|
|
+ if (!$this->config->get('auth.enabled')) {
|
|
|
|
+ return [
|
|
|
|
+ 'type' => 'noauth',
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ switch ($this->config->get('auth.in')) {
|
|
|
|
+ case "basic":
|
|
|
|
+ return [
|
|
|
|
+ 'type' => 'basic',
|
|
|
|
+ ];
|
|
|
|
+ case "bearer":
|
|
|
|
+ return [
|
|
|
|
+ 'type' => 'bearer',
|
|
|
|
+ ];
|
|
|
|
+ default:
|
|
|
|
+ return [
|
|
|
|
+ 'type' => 'apikey',
|
|
|
|
+ 'apikey' => [
|
|
|
|
+ [
|
|
|
|
+ 'key' => 'in',
|
|
|
|
+ 'value' => $this->config->get('auth.in'),
|
|
|
|
+ 'type' => 'string',
|
|
|
|
+ ],
|
|
|
|
+ [
|
|
|
|
+ 'key' => 'key',
|
|
|
|
+ 'value' => $this->config->get('auth.name'),
|
|
|
|
+ 'type' => 'string',
|
|
|
|
+ ],
|
|
|
|
+ ],
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
protected function generateEndpointItem($endpoint): array
|
|
protected function generateEndpointItem($endpoint): array
|
|
{
|
|
{
|
|
return [
|
|
return [
|
|
@@ -81,8 +118,9 @@ class PostmanCollectionWriter
|
|
'header' => $this->resolveHeadersForEndpoint($endpoint),
|
|
'header' => $this->resolveHeadersForEndpoint($endpoint),
|
|
'body' => empty($endpoint['bodyParameters']) ? null : $this->getBodyData($endpoint),
|
|
'body' => empty($endpoint['bodyParameters']) ? null : $this->getBodyData($endpoint),
|
|
'description' => $endpoint['metadata']['description'] ?? null,
|
|
'description' => $endpoint['metadata']['description'] ?? null,
|
|
- 'response' => [],
|
|
|
|
|
|
+ 'auth' => ($endpoint['metadata']['authenticated'] ?? false) ? null : ['type' => 'noauth'],
|
|
],
|
|
],
|
|
|
|
+ 'response' => [],
|
|
];
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
@@ -107,7 +145,7 @@ class PostmanCollectionWriter
|
|
$params = [
|
|
$params = [
|
|
'key' => $key,
|
|
'key' => $key,
|
|
'value' => $value,
|
|
'value' => $value,
|
|
- 'type' => 'text'
|
|
|
|
|
|
+ 'type' => 'text',
|
|
];
|
|
];
|
|
$body[$inputMode][] = $params;
|
|
$body[$inputMode][] = $params;
|
|
}
|
|
}
|
|
@@ -127,14 +165,13 @@ class PostmanCollectionWriter
|
|
return $body;
|
|
return $body;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
protected function resolveHeadersForEndpoint($route)
|
|
protected function resolveHeadersForEndpoint($route)
|
|
{
|
|
{
|
|
$headers = collect($route['headers']);
|
|
$headers = collect($route['headers']);
|
|
|
|
|
|
// Exclude authentication headers if they're handled by Postman auth
|
|
// Exclude authentication headers if they're handled by Postman auth
|
|
$authHeader = $this->getAuthHeader();
|
|
$authHeader = $this->getAuthHeader();
|
|
- if (! empty($authHeader)) {
|
|
|
|
|
|
+ if (!empty($authHeader)) {
|
|
$headers = $headers->except($authHeader);
|
|
$headers = $headers->except($authHeader);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -173,7 +210,7 @@ class PostmanCollectionWriter
|
|
}, $route['uri']),
|
|
}, $route['uri']),
|
|
'query' => collect($route['queryParameters'] ?? [])->map(function ($parameterData, $key) {
|
|
'query' => collect($route['queryParameters'] ?? [])->map(function ($parameterData, $key) {
|
|
// TODO remove: unneeded with new syntax
|
|
// TODO remove: unneeded with new syntax
|
|
- $key = rtrim($key,".*");
|
|
|
|
|
|
+ $key = rtrim($key, ".*");
|
|
return [
|
|
return [
|
|
'key' => $key,
|
|
'key' => $key,
|
|
'value' => urlencode($parameterData['value']),
|
|
'value' => urlencode($parameterData['value']),
|
|
@@ -186,10 +223,10 @@ class PostmanCollectionWriter
|
|
|
|
|
|
// Create raw url-parameter (Insomnia uses this on import)
|
|
// Create raw url-parameter (Insomnia uses this on import)
|
|
$query = collect($base['query'] ?? [])->map(function ($queryParamData) {
|
|
$query = collect($base['query'] ?? [])->map(function ($queryParamData) {
|
|
- return $queryParamData['key'].'='.$queryParamData['value'];
|
|
|
|
|
|
+ return $queryParamData['key'] . '=' . $queryParamData['value'];
|
|
})->implode('&');
|
|
})->implode('&');
|
|
$base['raw'] = sprintf('%s://%s/%s%s',
|
|
$base['raw'] = sprintf('%s://%s/%s%s',
|
|
- $base['protocol'], $base['host'], $base['path'], $query ? '?'.$query : null
|
|
|
|
|
|
+ $base['protocol'], $base['host'], $base['path'], $query ? '?' . $query : null
|
|
);
|
|
);
|
|
|
|
|
|
// If there aren't any url parameters described then return what we've got
|
|
// If there aren't any url parameters described then return what we've got
|
|
@@ -213,7 +250,7 @@ class PostmanCollectionWriter
|
|
protected function getAuthHeader()
|
|
protected function getAuthHeader()
|
|
{
|
|
{
|
|
$auth = $this->auth;
|
|
$auth = $this->auth;
|
|
- if (empty($auth) || ! is_string($auth['type'] ?? null)) {
|
|
|
|
|
|
+ if (empty($auth) || !is_string($auth['type'] ?? null)) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|