Skip to content

Commit

Permalink
change document title and control headers via config
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Feb 12, 2024
1 parent d7cf031 commit 0bda978
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
7 changes: 5 additions & 2 deletions config/request-docs.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

return [
// changes doc title
'title' => 'LRD - Laravel Request Docs',
'enabled' => true,
// change it to true will make lrd to throw exception if rules in request class need to be changed
// keep it false
Expand Down Expand Up @@ -38,8 +40,9 @@
'rules_methods' => [
'rules'
],
// Can be overridden as // @LRDresponses 200|400|401
'default_responses' => [ "200", "400", "401", "403", "404", "405", "422", "429", "500", "503"],

// changes default headers on first load for Set Global Headers
// Later the local storage is used when edits are made
'default_headers' => [
'Content-Type' => 'application/json',
],
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/LaravelRequestDocsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function assets(Request $request)
public function config(Request $request)
{
$config = [
'default_responses' => config('request-docs.default_responses'),
'title' => config('request-docs.title'),
'default_headers' => config('request-docs.default_headers'),
];
return response()->json($config);
Expand Down
11 changes: 6 additions & 5 deletions ui/src/components/ApiAction.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useEffect, useState } from 'react';

import useLocalStorage from 'react-use-localstorage';
import { defaultHeaders, makeCurlCommand } from '../libs/strings'
import type {IAPIInfo, LRDResponse} from '../libs/types'
import {makeCurlCommand } from '../libs/strings'
import type {IAPIInfo, LRDResponse, IConfig} from '../libs/types'
import ApiActionResponse from './elements/ApiActionResponse'
import ApiActionRequest from './elements/ApiActionRequest'
import ApiActionTabs from './elements/ApiActionTabs'
Expand All @@ -15,15 +15,16 @@ import { objectToFormData } from '../libs/object';
interface Props {
lrdDocsItem: IAPIInfo,
method: string,
host: string
host: string,
config: IConfig,
}
export default function ApiAction(props: Props) {
const { lrdDocsItem, method, host } = props
const { lrdDocsItem, method, host, config } = props
const [error, setError] = useState<string | null>(null);

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

const [requestHeaders, setRequestHeaders] = useLocalStorage('requestHeaders', defaultHeaders);
const [requestHeaders, setRequestHeaders] = useLocalStorage('requestHeaders', JSON.stringify(config.default_headers, null, 2));
const [curlCommand, setCurlCommand] = useState("");
const [requestUri, setRequestUri] = useState(lrdDocsItem.uri);
const [timeTaken, setTimeTaken] = useState(0);
Expand Down
26 changes: 23 additions & 3 deletions ui/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import ApiAction from './ApiAction';
import useLocalStorage from 'react-use-localstorage';
import shortid from 'shortid';
import Fuse from 'fuse.js';
import type { IAPIInfo } from '../libs/types'
import type { IAPIInfo, IConfig } from '../libs/types'


export default function App() {

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

if (!url) {
// get current url without query params
const domain = location.protocol + '//' + location.host
setHost(domain)
url = domain + "/request-docs/api"
configAPI = domain + "/request-docs/config"
}

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

const api = getUrl(url, showGet, showPost, showDelete, showPut, showPatch, showHead, sort, groupby)
generateDocs(api)
fetchConfig(configAPI)
}, [])

const scrollToAnchorOnHistory = () => {
Expand All @@ -70,7 +78,19 @@ export default function App() {
}
}
}

const fetchConfig = (url: string) => {
const response = fetch(url);
response
.then(c => c.json())
.then((c) => {
setConfig(c)
if (c.title && document) {
document.title = c.title
}
}).catch((error) => {
setError(error.message)
})
}
const generateDocs = (url: string) => {
setSendingRequest(true)
const response = fetch(url);
Expand Down Expand Up @@ -155,7 +175,7 @@ export default function App() {
<ApiInfo lrdDocsItem={lrdDocsItem} method={lrdDocsItem.http_method} />
</div>
<div className="col-span-5 ml-5">
<ApiAction lrdDocsItem={lrdDocsItem} method={lrdDocsItem.http_method} host={host} />
<ApiAction lrdDocsItem={lrdDocsItem} method={lrdDocsItem.http_method} host={host} config={config} />
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/TopNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function TopNav(props: Props) {
const [showDelete, setShowDelete] = useLocalStorage('showDelete', 'true');
const [showPut, setShowPut] = useLocalStorage('showPut', 'true');
const [showPatch, setShowPatch] = useLocalStorage('showPatch', 'true');
const [showHead, setShowHead] = useLocalStorage('showHead', 'true');
const [showHead, setShowHead] = useLocalStorage('showHead', 'false');

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const handleChangeGroupby = (e: any) => {
Expand Down
10 changes: 3 additions & 7 deletions ui/src/libs/strings.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

export const defaultHeaders = `{
"Content-Type": "application/json",
"Accept": "application/json"
}`
export const explode = (str: string, maxLength: number, by: string) => {
let buff = "";
const numOfLines = Math.floor(str.length / maxLength);
Expand All @@ -13,9 +9,9 @@ export const explode = (str: string, maxLength: number, by: string) => {
}

export const makeCurlCommand = (host:string, url: string, method: string, queries: string, headers: any): string => {

let curl = `curl`
curl += `\n -X ${method}`
curl += `\n -X ${method}`
try {
const jsonRequestHeaders = JSON.parse(headers)
for (const [key, value] of Object.entries(jsonRequestHeaders)) {
Expand All @@ -41,4 +37,4 @@ export const makeCurlCommand = (host:string, url: string, method: string, querie
return post(queries)
}
return ""
}
}
7 changes: 6 additions & 1 deletion ui/src/libs/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ export interface IAPIRule {
[key: string]: string[];
}

export interface IConfig {
title: string;
default_headers: string[];
}

export interface IAPIInfo {
uri: string;
middlewares: string[];
Expand All @@ -15,7 +20,7 @@ export interface IAPIInfo {
group: string;
group_index: number;
responses: string[];

}

export interface LRDResponse {
Expand Down

0 comments on commit 0bda978

Please sign in to comment.