Skip to content

Commit

Permalink
feat: remove need for authedrequest, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Oct 2, 2023
1 parent 4a67427 commit 3cefef5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/lib/client-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { createClient } from '../../core/utils/api/client.js'
import type { createClient } from '../../core/utils/api/client.js'

export type Client = ReturnType<typeof createClient>

Expand Down
83 changes: 20 additions & 63 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,52 +154,6 @@ export class PubPub {
return response
}

async authedRequest<
T extends Record<string, any> | string | void = Record<string, any> | string
>(path: string, method: 'GET', options?: RequestInit): Promise<T>
async authedRequest<
T extends Record<string, any> | string | void = Record<string, any> | string
>(
path: string,
method: 'POST' | 'PATCH' | 'PUT' | 'DELETE',
body?: Record<string, any>,
options?: RequestInit
): Promise<T>
async authedRequest<
T extends Record<string, any> | string | void = Record<string, any> | string
>(
path: string,
method: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE',
bodyOrOptions: Record<string, any> | RequestInit,
optionsMabye?: RequestInit
): Promise<T> {
const options = method === 'GET' ? bodyOrOptions : optionsMabye
const body = method !== 'GET' ? JSON.stringify(bodyOrOptions) : undefined

const response = await fetch(`${this.communityUrl}/api/${path}`, {
body,
// body,
method,
...options,
headers: {
...options?.headers,
'Content-Type': 'application/json',
Cookie: this.#cookie || '',
},
})

if (!response.ok || response.status < 200 || response.status >= 300) {
console.log(response)
throw new Error(
`Request failed with status ${response.status}: ${response.statusText}`
)
}

const data = await response.json()

return data
}

/**
* Get the HTML content of a page
*/
Expand Down Expand Up @@ -1121,7 +1075,9 @@ export class PubPub {
}
})

putPayload.navigation &&= putPayload.navigation.map((link) => {
function fixLinks(
link: NonNullable<CommunityPutPayload['navigation']>[0]
) {
if ('id' in link) {
return link
}
Expand All @@ -1130,21 +1086,27 @@ export class PubPub {

return {
id,

...link,
}
})
}

let response: Partial<
CommunityPutResponse & { facets: (typeof facetsPayload)['facets'] }
> = {}

const { navigation, footerLinks, ...rest } = putPayload

if (shouldPutCommunity) {
const communityResponse = (await this.authedRequest(
`community/${this.communityId}`,
'PUT',
putPayload
)) as CommunityPutResponse
const communityResponse = await this.client.community.update({
...rest,
id: this.communityId,
...(navigation && {
navigation: navigation.map(fixLinks),
}),
...(footerLinks && {
footerLinks: footerLinks.map(fixLinks),
}),
})
response = { ...response, ...communityResponse }
}

Expand All @@ -1165,15 +1127,10 @@ export class PubPub {
* Update the global community CSS
*/
css: async (css: string) => {
const response = await this.authedRequest<Record<string, never>>(
'customScripts',
'POST',
{
communityId: this.communityId,
type: 'css',
content: css,
}
)
const response = await this.client.customScript.set({
type: 'css',
content: css,
})

return response
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/initialData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CollectionKind } from './types.js'
import type { CollectionKind } from './types.js'

export type InitialData = {
communityData: CommunityData
Expand Down
76 changes: 31 additions & 45 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Collection } from './collectionData.js'
import { ResourceWarning } from './editor/types.js'
import { DefinitelyHas } from './type-helpers.js'
import { ProposedMetadata } from './firebase/rest/firebase.js'
import { DeepInput } from './client-types.js'
import type { Collection } from './collectionData.js'
import type { ResourceWarning } from './editor/types.js'
import type { DefinitelyHas } from './type-helpers.js'
import type { ProposedMetadata } from './firebase/rest/firebase.js'
import type { DeepInput } from './client-types.js'

const DEFAULT_ROLES = [
'Conceptualization',
Expand Down Expand Up @@ -153,13 +153,36 @@ export type CommunityPutResponse = {
}

export type CommunityPutPayload = Omit<
CommunityPutResponse,
DeepInput<'community.update'>,
'navigation' | 'footerLinks'
> & {
navigation?: NavigationLink<'request'>[]
footerLinks?: FooterLink<'request'>[]
/**
* Navigation links.
*
* These can either take an id, which will be used to update an existing link,
* or omit an id, which will create a new link.
*
* Options:
* - DropDownMenu
* - PageOrCollectionLink
* - ExternalLink
*/
navigation?: (NavigationLink<'request'> | NavigationLink<'response'>)[]
/**
* Footer links. These can either take an id, which will be used to update an existing link,
* or omit an id, which will create a new link.
*/
footerLinks?: (FooterLink<'request'> | FooterLink<'response'>)[]
}

// export type CommunityPutPayload = Omit<
// CommunityPutResponse,
// 'navigation' | 'footerLinks'
// > & {
// navigation?: NavigationLink<'request'>[]
// footerLinks?: FooterLink<'request'>[]
// }

type NavigationLink<T extends 'request' | 'response' = 'response'> =
| (T extends 'request' ? Omit<DropDownMenu, 'id'> : DropDownMenu)
| PageOrCollectionLink
Expand Down Expand Up @@ -191,13 +214,6 @@ type Child = PageOrCollectionLink | ExternalLink

export type SourceFile = DeepInput<'import'>['sourceFiles'][number]

export type WorkerTaskResponse = {
id: string
isProcessing: boolean
error?: any
output?: unknown
}

export type WorkerTaskExportOutput = {
url: string
}
Expand All @@ -209,12 +225,6 @@ export type WorkerTaskImportOutput = {
pandocErrorOutput: string
}

export type PubsManyResponse = {
pubIds: string[]
pubsById: PubsById
loadedAllPubs: boolean
}

export type PubsById = {
[key: string]: Pub
}
Expand Down Expand Up @@ -540,30 +550,6 @@ export type Visibility = {
users: VisibilityUser[]
}

export type TaggedVisibilityParent =
| { type: 'discussion'; value: Discussion }
| { type: 'review'; value: Review }

export type Review = {
id: string
author: User
createdAt: string
updatedAt: string
title: string
number: number
status: 'open' | 'closed' | 'completed'
releaseRequested: boolean
threadId: string
thread: Thread
visibilityId: string
visibility?: Visibility
userId: string
pubId: string
pub?: Pub
reviewContent?: DocJson
reviewers?: Reviewer[]
}

export type Reviewer = {
id: string
name: string
Expand Down
4 changes: 2 additions & 2 deletions src/lib/viewData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection } from './collectionData.js'
import {
import type { Collection } from './collectionData.js'
import type {
CollectionPubWithAttributions,
Discussion,
Pub,
Expand Down
4 changes: 3 additions & 1 deletion test/test.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import { describe, it, beforeAll, expect, afterAll } from 'vitest'
import { PubPubSDK } from '../src/lib/client.js'
import type { PubPubSDK } from '../src/lib/client.js'
import { setupSDK } from './utils/setup.js'
import dotenv from 'dotenv'
dotenv.config()
Expand Down Expand Up @@ -186,6 +186,8 @@ describe('PubPub', () => {
expect(modded.body).toHaveProperty('description')
}, 10000)

it('should be able to update a community', async () => {})

it('should remove a pub', async () => {
const remove = await pubpub.pub.remove({ pubId: pub.id })

Expand Down

0 comments on commit 3cefef5

Please sign in to comment.