Skip to content

Commit 0bda978

Browse files
change document title and control headers via config
1 parent d7cf031 commit 0bda978

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

config/request-docs.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
return [
4+
// changes doc title
5+
'title' => 'LRD - Laravel Request Docs',
46
'enabled' => true,
57
// change it to true will make lrd to throw exception if rules in request class need to be changed
68
// keep it false
@@ -38,8 +40,9 @@
3840
'rules_methods' => [
3941
'rules'
4042
],
41-
// Can be overridden as // @LRDresponses 200|400|401
42-
'default_responses' => [ "200", "400", "401", "403", "404", "405", "422", "429", "500", "503"],
43+
44+
// changes default headers on first load for Set Global Headers
45+
// Later the local storage is used when edits are made
4346
'default_headers' => [
4447
'Content-Type' => 'application/json',
4548
],

src/Controllers/LaravelRequestDocsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function assets(Request $request)
130130
public function config(Request $request)
131131
{
132132
$config = [
133-
'default_responses' => config('request-docs.default_responses'),
133+
'title' => config('request-docs.title'),
134134
'default_headers' => config('request-docs.default_headers'),
135135
];
136136
return response()->json($config);

ui/src/components/ApiAction.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { useEffect, useState } from 'react';
22

33
import useLocalStorage from 'react-use-localstorage';
4-
import { defaultHeaders, makeCurlCommand } from '../libs/strings'
5-
import type {IAPIInfo, LRDResponse} from '../libs/types'
4+
import {makeCurlCommand } from '../libs/strings'
5+
import type {IAPIInfo, LRDResponse, IConfig} from '../libs/types'
66
import ApiActionResponse from './elements/ApiActionResponse'
77
import ApiActionRequest from './elements/ApiActionRequest'
88
import ApiActionTabs from './elements/ApiActionTabs'
@@ -15,15 +15,16 @@ import { objectToFormData } from '../libs/object';
1515
interface Props {
1616
lrdDocsItem: IAPIInfo,
1717
method: string,
18-
host: string
18+
host: string,
19+
config: IConfig,
1920
}
2021
export default function ApiAction(props: Props) {
21-
const { lrdDocsItem, method, host } = props
22+
const { lrdDocsItem, method, host, config } = props
2223
const [error, setError] = useState<string | null>(null);
2324

2425
const [allParamsRegistry, setAllParamsRegistery] = useLocalStorage('allParamsRegistry', "{}");
2526

26-
const [requestHeaders, setRequestHeaders] = useLocalStorage('requestHeaders', defaultHeaders);
27+
const [requestHeaders, setRequestHeaders] = useLocalStorage('requestHeaders', JSON.stringify(config.default_headers, null, 2));
2728
const [curlCommand, setCurlCommand] = useState("");
2829
const [requestUri, setRequestUri] = useState(lrdDocsItem.uri);
2930
const [timeTaken, setTimeTaken] = useState(0);

ui/src/components/App.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import ApiAction from './ApiAction';
77
import useLocalStorage from 'react-use-localstorage';
88
import shortid from 'shortid';
99
import Fuse from 'fuse.js';
10-
import type { IAPIInfo } from '../libs/types'
10+
import type { IAPIInfo, IConfig } from '../libs/types'
1111

1212

1313
export default function App() {
1414

1515
const [lrdDocsJson, setLrdDocsJson] = useState<IAPIInfo[]>([]);
1616
const [lrdDocsJsonCopy, setLrdDocsJsonCopy] = useState<IAPIInfo[]>([]);
1717
const [apiURL, setApiURL] = useState<string>('');
18+
const [config, setConfig] = useState<IConfig>({
19+
title: "",
20+
default_headers: ["Content-Type: application/json", "Accept: application/json"]
21+
});
1822
const [host, setHost] = useState<string>('');
1923
const [sendingRequest, setSendingRequest] = useState(false);
2024
const [error, setError] = useState<string | null>(null);
@@ -40,23 +44,27 @@ export default function App() {
4044
// get query param named api
4145
const urlParams = new URLSearchParams(window.location.search);
4246
let url = urlParams.get('api');
47+
let configAPI = ""
4348

4449
if (!url) {
4550
// get current url without query params
4651
const domain = location.protocol + '//' + location.host
4752
setHost(domain)
4853
url = domain + "/request-docs/api"
54+
configAPI = domain + "/request-docs/config"
4955
}
5056

5157
if (url) {
5258
// extract host from url
5359
const domain = url?.split('/').slice(0, 3).join('/');
5460
setHost(domain)
61+
configAPI = domain + "/request-docs/config"
5562
}
5663
setApiURL(url)
5764

5865
const api = getUrl(url, showGet, showPost, showDelete, showPut, showPatch, showHead, sort, groupby)
5966
generateDocs(api)
67+
fetchConfig(configAPI)
6068
}, [])
6169

6270
const scrollToAnchorOnHistory = () => {
@@ -70,7 +78,19 @@ export default function App() {
7078
}
7179
}
7280
}
73-
81+
const fetchConfig = (url: string) => {
82+
const response = fetch(url);
83+
response
84+
.then(c => c.json())
85+
.then((c) => {
86+
setConfig(c)
87+
if (c.title && document) {
88+
document.title = c.title
89+
}
90+
}).catch((error) => {
91+
setError(error.message)
92+
})
93+
}
7494
const generateDocs = (url: string) => {
7595
setSendingRequest(true)
7696
const response = fetch(url);
@@ -155,7 +175,7 @@ export default function App() {
155175
<ApiInfo lrdDocsItem={lrdDocsItem} method={lrdDocsItem.http_method} />
156176
</div>
157177
<div className="col-span-5 ml-5">
158-
<ApiAction lrdDocsItem={lrdDocsItem} method={lrdDocsItem.http_method} host={host} />
178+
<ApiAction lrdDocsItem={lrdDocsItem} method={lrdDocsItem.http_method} host={host} config={config} />
159179
</div>
160180
</div>
161181
</div>

ui/src/components/TopNav.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function TopNav(props: Props) {
3838
const [showDelete, setShowDelete] = useLocalStorage('showDelete', 'true');
3939
const [showPut, setShowPut] = useLocalStorage('showPut', 'true');
4040
const [showPatch, setShowPatch] = useLocalStorage('showPatch', 'true');
41-
const [showHead, setShowHead] = useLocalStorage('showHead', 'true');
41+
const [showHead, setShowHead] = useLocalStorage('showHead', 'false');
4242

4343
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4444
const handleChangeGroupby = (e: any) => {

ui/src/libs/strings.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11

2-
export const defaultHeaders = `{
3-
"Content-Type": "application/json",
4-
"Accept": "application/json"
5-
}`
62
export const explode = (str: string, maxLength: number, by: string) => {
73
let buff = "";
84
const numOfLines = Math.floor(str.length / maxLength);
@@ -13,9 +9,9 @@ export const explode = (str: string, maxLength: number, by: string) => {
139
}
1410

1511
export const makeCurlCommand = (host:string, url: string, method: string, queries: string, headers: any): string => {
16-
12+
1713
let curl = `curl`
18-
curl += `\n -X ${method}`
14+
curl += `\n -X ${method}`
1915
try {
2016
const jsonRequestHeaders = JSON.parse(headers)
2117
for (const [key, value] of Object.entries(jsonRequestHeaders)) {
@@ -41,4 +37,4 @@ export const makeCurlCommand = (host:string, url: string, method: string, querie
4137
return post(queries)
4238
}
4339
return ""
44-
}
40+
}

ui/src/libs/types.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ export interface IAPIRule {
22
[key: string]: string[];
33
}
44

5+
export interface IConfig {
6+
title: string;
7+
default_headers: string[];
8+
}
9+
510
export interface IAPIInfo {
611
uri: string;
712
middlewares: string[];
@@ -15,7 +20,7 @@ export interface IAPIInfo {
1520
group: string;
1621
group_index: number;
1722
responses: string[];
18-
23+
1924
}
2025

2126
export interface LRDResponse {

0 commit comments

Comments
 (0)