@@ -48,74 +48,62 @@ docs-api/
4848``` typescript
4949#! / usr / bin / env deno run -- allow - net -- allow - env
5050
51- import { doc } from " jsr:@deno/doc" ;
51+ import { doc } from ' jsr:@deno/doc'
5252
5353interface GenerateRequest {
54- package: string ;
55- version: string ;
54+ package: string
55+ version: string
5656}
5757
5858function validateAuth(req : Request ): boolean {
59- const authHeader = req .headers .get (" Authorization" );
60- const expectedToken = Deno .env .get (" API_SECRET" );
61- if (! expectedToken ) return true ;
62- return authHeader === ` Bearer ${expectedToken } ` ;
59+ const authHeader = req .headers .get (' Authorization' )
60+ const expectedToken = Deno .env .get (' API_SECRET' )
61+ if (! expectedToken ) return true
62+ return authHeader === ` Bearer ${expectedToken } `
6363}
6464
6565export default async function handler(req : Request ): Promise <Response > {
6666 const headers = {
67- " Access-Control-Allow-Origin" : " https://npmx.dev" ,
68- " Access-Control-Allow-Methods" : " POST, OPTIONS" ,
69- " Access-Control-Allow-Headers" : " Content-Type, Authorization" ,
70- " Content-Type" : " application/json" ,
71- };
72-
73- if (req .method === " OPTIONS" ) {
74- return new Response (null , { status: 204 , headers });
67+ ' Access-Control-Allow-Origin' : ' https://npmx.dev' ,
68+ ' Access-Control-Allow-Methods' : ' POST, OPTIONS' ,
69+ ' Access-Control-Allow-Headers' : ' Content-Type, Authorization' ,
70+ ' Content-Type' : ' application/json' ,
71+ }
72+
73+ if (req .method === ' OPTIONS' ) {
74+ return new Response (null , { status: 204 , headers })
7575 }
7676
77- if (req .method !== " POST" ) {
78- return new Response (
79- JSON .stringify ({ error: " method_not_allowed" }),
80- { status: 405 , headers }
81- );
77+ if (req .method !== ' POST' ) {
78+ return new Response (JSON .stringify ({ error: ' method_not_allowed' }), { status: 405 , headers })
8279 }
8380
8481 if (! validateAuth (req )) {
85- return new Response (
86- JSON .stringify ({ error: " unauthorized" }),
87- { status: 401 , headers }
88- );
82+ return new Response (JSON .stringify ({ error: ' unauthorized' }), { status: 401 , headers })
8983 }
9084
9185 try {
92- const body: GenerateRequest = await req .json ();
93-
86+ const body: GenerateRequest = await req .json ()
87+
9488 if (! body .package || ! body .version ) {
95- return new Response (
96- JSON .stringify ({ error: " bad_request" }),
97- { status: 400 , headers }
98- );
89+ return new Response (JSON .stringify ({ error: ' bad_request' }), { status: 400 , headers })
9990 }
10091
101- const specifier = ` https://esm.sh/${body .package }@${body .version }?target=deno ` ;
102- const nodes = await doc (specifier );
103-
104- return new Response (JSON .stringify ({ nodes }), { status: 200 , headers });
92+ const specifier = ` https://esm.sh/${body .package }@${body .version }?target=deno `
93+ const nodes = await doc (specifier )
94+
95+ return new Response (JSON .stringify ({ nodes }), { status: 200 , headers })
10596 } catch (error ) {
106- const message = error instanceof Error ? error .message : " Unknown error" ;
107-
108- if (message .includes (" Could not find" )) {
109- return new Response (
110- JSON .stringify ({ error: " not_found" }),
111- { status: 404 , headers }
112- );
97+ const message = error instanceof Error ? error .message : ' Unknown error'
98+
99+ if (message .includes (' Could not find' )) {
100+ return new Response (JSON .stringify ({ error: ' not_found' }), { status: 404 , headers })
113101 }
114-
115- return new Response (
116- JSON . stringify ({ error: " generation_failed " , message }) ,
117- { status: 500 , headers }
118- );
102+
103+ return new Response (JSON . stringify ({ error: ' generation_failed ' , message }), {
104+ status: 500 ,
105+ headers ,
106+ })
119107 }
120108}
121109```
@@ -139,7 +127,7 @@ async function runDenoDoc(packageName: string, version: string): Promise<DenoDoc
139127 const headers: Record <string , string > = {
140128 ' Content-Type' : ' application/json' ,
141129 }
142-
130+
143131 if (DOCS_API_SECRET ) {
144132 headers [' Authorization' ] = ` Bearer ${DOCS_API_SECRET } `
145133 }
@@ -158,7 +146,7 @@ async function runDenoDoc(packageName: string, version: string): Promise<DenoDoc
158146 throw new Error (` Docs API error: ${error .message } ` )
159147 }
160148
161- return await response .json () as DenoDocResult
149+ return ( await response .json () ) as DenoDocResult
162150}
163151
164152export async function generateDocsWithDeno(
@@ -186,6 +174,7 @@ export async function generateDocsWithDeno(
186174#### Remove Unused Code
187175
188176Delete from ` server/utils/docs.ts ` :
177+
189178- ` execFileAsync ` import
190179- ` DENO_DOC_TIMEOUT_MS ` , ` DENO_DOC_MAX_BUFFER ` constants
191180- ` denoCheckPromise ` , ` isDenoInstalled() ` , ` verifyDenoInstalled() `
@@ -198,7 +187,7 @@ Keep subprocess as fallback for local dev:
198187
199188``` typescript
200189async function runDenoDoc(packageName : string , version : string ): Promise <DenoDocResult > {
201- if (process .dev && await isDenoInstalled ()) {
190+ if (process .dev && ( await isDenoInstalled () )) {
202191 return runLocalDenoDoc (packageName , version )
203192 }
204193 return runRemoteDenoDoc (packageName , version )
0 commit comments