Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/agency.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Brand, Domains } from './resources/agency'
import { Brand, Domains, Projects } from './resources/agency'

import type { Blutui } from './blutui'
import type { GetOptions, PostOptions } from './types'

export class Agency {
readonly brand = new Brand(this)
readonly domains = new Domains(this)
readonly projects = new Projects(this)

constructor(
public username: string,
Expand Down
23 changes: 16 additions & 7 deletions src/resources/agency/domains/domains.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fetch from 'jest-fetch-mock'
import { Blutui } from '@/blutui'
import { fetchOnce, fetchURL } from '@/utils/testing'

import domainFixture from './fixtures/domain.json'
import domainListFixture from './fixtures/domainList.json'
import domainWithProjectFixture from './fixtures/domain-with-project.json'
import domainListFixture from './fixtures/domain-list.json'

const accessToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
Expand Down Expand Up @@ -34,21 +36,28 @@ describe('Domain', () => {
expect(domain).toMatchObject({
object: 'domain',
})
expect(typeof domain.project).toBe('string')
})

it('can retrieve a domain information with project', async () => {
fetchOnce(domainFixture)
const domain = await blutui.agency('foo').domains.get(domainFixture.id, {
expand: ['project'],
})
fetchOnce(domainWithProjectFixture)
const domain = await blutui
.agency('foo')
.domains.get(domainWithProjectFixture.id, {
expand: ['project'],
})
expect(fetchURL()).toContain(
encodeURI(
`/v1/agencies/foo/domains/${domainFixture.id}?expand[]=project`
`/v1/agencies/foo/domains/${domainWithProjectFixture.id}?expand[]=project`
)
)
expect(domain).toMatchObject({
object: 'domain',
})
expect(domain.project).toMatchObject({
id: '99bc147e-966c-4dd0-8def-de817c63cf41',
createdAt: 1720758022,
})
})
})

Expand Down Expand Up @@ -117,7 +126,7 @@ describe('Domain', () => {
})

describe('search', () => {
it('Search for domains in your agency.', async () => {
it('can search for domains', async () => {
fetchOnce(domainListFixture)
await blutui.agency('foo').domains.search({ name: 'example.com' })

Expand Down
16 changes: 8 additions & 8 deletions src/resources/agency/domains/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
} from './interfaces'
import type {
DeletedResponse,
Expandable,
List,
ListResponse,
PaginationOptions,
Expand All @@ -30,18 +31,19 @@ export class Domains {
/**
* Get the domains list for the current agency.
*/
async list(paginationOptions?: PaginationOptions): Promise<List<Domain>> {
async list(options?: PaginationOptions): Promise<List<Domain>> {
const { data } = await this.agency.get<ListResponse<DomainResponse>>(
'domains',
{ query: paginationOptions }
{ query: options }
)

return deserializeDomainList(data)
}

/**
* Get a domain information by id.
* Get a domain's information by ID.
*/
async get(id: string, options?: { expand: string[] }): Promise<Domain> {
async get(id: string, options?: Expandable<'project'>): Promise<Domain> {
const { data } = await this.agency.get<DomainResponse>(`domains/${id}`, {
query: options,
})
Expand Down Expand Up @@ -110,13 +112,11 @@ export class Domains {
/**
* Search for domains in your agency.
*/
async search(
searchDomainOptions: SearchDomainOptions
): Promise<List<Domain>> {
async search(payload: SearchDomainOptions): Promise<List<Domain>> {
const { data } = await this.agency.post<
ListResponse<DomainResponse>,
SerializedSearchDomainOptions
>('domains/search', searchDomainOptions)
>('domains/search', payload)

return deserializeDomainList(data)
}
Expand Down
25 changes: 25 additions & 0 deletions src/resources/agency/domains/fixtures/domain-with-project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "9bfdb42b-1bf0-4510-978e-46aa329f8efa",
"object": "domain",
"name": "example.com",
"token": "08Q8wwsDMIuCwAsudZtXgf3ABkAwqbExgUPWUEPEuMBoWUIH0ie81R27h2elqjk1",
"project": {
"id": "99bc147e-966c-4dd0-8def-de817c63cf41",
"object": "project",
"name": "One",
"description": "One",
"image": null,
"handle": "one",
"password": "SorKVQWV",
"timezone": "UTC",
"subdomain": "one",
"primary_domain": "9bfdb42b-1bf0-4510-978e-46aa329f8efa",
"published": true,
"processed": true,
"created_at": 1720758022,
"updated_at": 1720758046
},
"verified_at": null,
"created_at": 1716170007,
"updated_at": 1716170007
}
10 changes: 6 additions & 4 deletions src/resources/agency/domains/interfaces/domain.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Project, ProjectResponse } from '../../projects/interfaces'

export interface Domain {
id: string
object: 'domain'
name: string
token: string
project: string | Record<string, unknown>
verifiedAt: number
project: string | null | Project
verifiedAt: number | null
createdAt: number
updatedAt: number
}
Expand All @@ -14,8 +16,8 @@ export interface DomainResponse {
object: 'domain'
name: string
token: string
project: string | Record<string, unknown>
verified_at: number
project: string | null | ProjectResponse
verified_at: number | null
created_at: number
updated_at: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { deserializePaginationMeta } from '@/utils/serializers'

import type { List, ListResponse } from '@/types'
import type { Domain, DomainResponse } from '../interfaces'
import { deserializeProject } from '../../projects/serializers'

export const deserializeDomain = (domain: DomainResponse): Domain => ({
id: domain.id,
object: domain.object,
name: domain.name,
token: domain.token,
project: domain.project,
project:
domain.project instanceof Object
? deserializeProject(domain.project)
: domain.project,
verifiedAt: domain.verified_at,
createdAt: domain.created_at,
updatedAt: domain.updated_at,
Expand Down
1 change: 1 addition & 0 deletions src/resources/agency/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Brand } from './brand/brand'
export { Domains } from './domains/domains'
export { Projects } from './projects/projects'
Empty file.
13 changes: 0 additions & 13 deletions src/resources/agency/project/interfaces/project.interface.ts

This file was deleted.

30 changes: 30 additions & 0 deletions src/resources/agency/projects/fixtures/project-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"object": "list",
"data": [
{
"id": "99bc147e-966c-4dd0-8def-de817c63cf41",
"object": "project",
"name": "One",
"description": "One",
"image": null,
"handle": "one",
"password": "SorKVQWV",
"timezone": "UTC",
"subdomain": "one",
"primary_domain": "9bfdb42b-1bf0-4510-978e-46aa329f8efa",
"published": true,
"processed": true,
"created_at": 1720758022,
"updated_at": 1720758046
}
],
"meta": {
"hasMore": false,
"currentPage": 1,
"from": 1,
"to": 1,
"perPage": 10,
"total": 1,
"lastPage": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "99bc147e-966c-4dd0-8def-de817c63cf41",
"object": "project",
"name": "One",
"description": "One",
"image": null,
"handle": "one",
"password": "SorKVQWV",
"timezone": "UTC",
"subdomain": "one",
"primary_domain": {
"id": "9bfdb42b-1bf0-4510-978e-46aa329f8efa",
"object": "domain",
"name": "example.com",
"token": "08Q8wwsDMIuCwAsudZtXgf3ABkAwqbExgUPWUEPEuMBoWUIH0ie81R27h2elqjk1",
"project": "9bf15409-db06-4c2c-b1ee-3a64c0074092",
"verified_at": null,
"created_at": 1716170007,
"updated_at": 1716170007
},
"published": true,
"processed": true,
"created_at": 1720758022,
"updated_at": 1720758046
}
16 changes: 16 additions & 0 deletions src/resources/agency/projects/fixtures/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "99bc147e-966c-4dd0-8def-de817c63cf41",
"object": "project",
"name": "One",
"description": "One",
"image": null,
"handle": "one",
"password": "SorKVQWV",
"timezone": "UTC",
"subdomain": "one",
"primary_domain": "9bfdb42b-1bf0-4510-978e-46aa329f8efa",
"published": true,
"processed": true,
"created_at": 1720758022,
"updated_at": 1720758046
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface CreateProjectOptions {
name: string
handle?: string
timezone?: string
description?: string | null
subdomain?: string
}

export interface SerializedCreateProjectOptions {
name: string
handle?: string
timezone?: string
description?: string | null
subdomain?: string
}
4 changes: 4 additions & 0 deletions src/resources/agency/projects/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './create-project-options.interface'
export * from './project.interface'
export * from './search-project-options.interface'
export * from './update-project-options.interface'
37 changes: 37 additions & 0 deletions src/resources/agency/projects/interfaces/project.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Domain, DomainResponse } from '../../domains/interfaces'

export interface Project {
id: string
object: 'project'
name: string
description: string | null
image: string | null
handle: string
password: string
timezone: string
subdomain: string
primaryDomain: string | null | Domain
published: boolean
processed: boolean
createdAt: number
updatedAt: number
deletedAt?: number
}

export interface ProjectResponse {
id: string
object: 'project'
name: string
description: string | null
image: string | null
handle: string
password: string
timezone: string
subdomain: string
primary_domain: string | null | DomainResponse
published: boolean
processed: boolean
created_at: number
updated_at: number
deleted_at?: number
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface SearchProjectOptions {
name: string
}

export interface SerializedSearchProjectOptions {
name: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface UpdateProjectOptions {
name?: string
description?: string | null
password?: string
timezone?: string
primaryDomain?: string | null
}

export interface SerializedUpdateProjectOptions {
name?: string
description?: string | null
password?: string
timezone?: string
primary_domain?: string | null
}
Loading