Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add listStores method #149

Merged
merged 6 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Expand Up @@ -23,6 +23,7 @@ module.exports = {
'unicorn/prefer-ternary': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['error', { ignoreRestSiblings: true }],
'func-style': 'off',
},
overrides: [
...overrides,
Expand Down
4 changes: 4 additions & 0 deletions src/backend/list_stores.ts
@@ -0,0 +1,4 @@
export interface ListStoresResponse {
stores: string[]
next_cursor?: string
}
28 changes: 18 additions & 10 deletions src/client.ts
Expand Up @@ -12,7 +12,7 @@ interface MakeStoreRequestOptions {
metadata?: Metadata
method: HTTPMethod
parameters?: Record<string, string>
storeName: string
storeName?: string
}

export interface ClientOptions {
Expand All @@ -31,7 +31,7 @@ interface GetFinalRequestOptions {
metadata?: Metadata
method: string
parameters?: Record<string, string>
storeName: string
storeName?: string
}

export class Client {
Expand Down Expand Up @@ -70,6 +70,16 @@ export class Client {
const encodedMetadata = encodeMetadata(metadata)
const consistency = opConsistency ?? this.consistency

let urlPath = `/${this.siteID}`

if (storeName) {
urlPath += `/${storeName}`
}

if (key) {
urlPath += `/${key}`
}

if (this.edgeURL) {
if (consistency === 'strong' && !this.uncachedEdgeURL) {
throw new BlobsConsistencyError()
Expand All @@ -83,8 +93,7 @@ export class Client {
headers[METADATA_HEADER_INTERNAL] = encodedMetadata
}

const path = key ? `/${this.siteID}/${storeName}/${key}` : `/${this.siteID}/${storeName}`
const url = new URL(path, consistency === 'strong' ? this.uncachedEdgeURL : this.edgeURL)
const url = new URL(urlPath, consistency === 'strong' ? this.uncachedEdgeURL : this.edgeURL)

for (const key in parameters) {
url.searchParams.set(key, parameters[key])
Expand All @@ -97,23 +106,22 @@ export class Client {
}

const apiHeaders: Record<string, string> = { authorization: `Bearer ${this.token}` }
const url = new URL(`/api/v1/blobs/${this.siteID}/${storeName}`, this.apiURL ?? 'https://api.netlify.com')
const url = new URL(`/api/v1/blobs${urlPath}`, this.apiURL ?? 'https://api.netlify.com')

for (const key in parameters) {
url.searchParams.set(key, parameters[key])
}

// If there is no key, we're dealing with the list endpoint, which is
// implemented directly in the Netlify API.
if (key === undefined) {
// If there is no store name, we're listing stores. If there's no key,
// we're listing blobs. Both operations are implemented directly in the
// Netlify API.
if (storeName === undefined || key === undefined) {
return {
headers: apiHeaders,
url: url.toString(),
}
}

url.pathname += `/${key}`

if (encodedMetadata) {
apiHeaders[METADATA_HEADER_EXTERNAL] = encodedMetadata
}
Expand Down
18 changes: 9 additions & 9 deletions src/consistency.test.ts
Expand Up @@ -52,17 +52,17 @@ describe('Consistency configuration', () => {
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value),
url: `${uncachedEdgeURL}/${siteID}/production/${key}`,
url: `${uncachedEdgeURL}/${siteID}/site:production/${key}`,
})
.head({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(null, { headers }),
url: `${uncachedEdgeURL}/${siteID}/production/${key}`,
url: `${uncachedEdgeURL}/${siteID}/site:production/${key}`,
})
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value, { headers }),
url: `${uncachedEdgeURL}/${siteID}/production/${key}`,
url: `${uncachedEdgeURL}/${siteID}/site:production/${key}`,
})

globalThis.fetch = mockStore.fetch
Expand Down Expand Up @@ -107,17 +107,17 @@ describe('Consistency configuration', () => {
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value),
url: `${uncachedEdgeURL}/${siteID}/production/${key}`,
url: `${uncachedEdgeURL}/${siteID}/site:production/${key}`,
})
.head({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(null, { headers }),
url: `${uncachedEdgeURL}/${siteID}/production/${key}`,
url: `${uncachedEdgeURL}/${siteID}/site:production/${key}`,
})
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value, { headers }),
url: `${uncachedEdgeURL}/${siteID}/production/${key}`,
url: `${uncachedEdgeURL}/${siteID}/site:production/${key}`,
})

globalThis.fetch = mockStore.fetch
Expand Down Expand Up @@ -213,17 +213,17 @@ describe('Consistency configuration', () => {
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value),
url: `${uncachedEdgeURL}/${siteID}/production/${key}`,
url: `${uncachedEdgeURL}/${siteID}/site:production/${key}`,
})
.head({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(null, { headers }),
url: `${edgeURL}/${siteID}/production/${key}`,
url: `${edgeURL}/${siteID}/site:production/${key}`,
})
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value, { headers }),
url: `${edgeURL}/${siteID}/production/${key}`,
url: `${edgeURL}/${siteID}/site:production/${key}`,
})

globalThis.fetch = mockStore.fetch
Expand Down
4 changes: 2 additions & 2 deletions src/lambda_compat.test.ts
Expand Up @@ -48,12 +48,12 @@ describe('With edge credentials', () => {
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value),
url: `${edgeURL}/${siteID}/production/${key}`,
url: `${edgeURL}/${siteID}/site:production/${key}`,
})
.get({
headers: { authorization: `Bearer ${edgeToken}` },
response: new Response(value),
url: `${edgeURL}/${siteID}/production/${key}`,
url: `${edgeURL}/${siteID}/site:production/${key}`,
})

globalThis.fetch = mockStore.fetch
Expand Down