Skip to content

Commit

Permalink
Fix lint staged and lint everything
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelreichor committed Oct 29, 2024
1 parent 0b0d5c5 commit 23b3a17
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-kids-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'js-craft-api': patch
---

Fix lint staged and lint everything
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ npm install js-craft-api

## Contributing

Contributions are welcome! <3
Contributions are welcome! <3
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"vitest": "^2.1.3"
},
"lint-staged": {
"*.--write": "npm run format"
"*.{js,vue,ts}": "npm run format"
}
}
33 changes: 16 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getPreviewToken } from './utils/helper';

export type ElementType = 'addresses' | 'assets' | 'entries' | 'users';
export type ExecutionMethods = 'all' | 'one'
export type ExecutionMethods = 'all' | 'one';

// Common query parameters shared by all element types, including allowed default methods
export interface CommonQueryParams {
Expand Down Expand Up @@ -52,7 +52,11 @@ export interface UserQueryParams {
}

// Merge Queryparams for better dx
export type MergedQueryParams = CommonQueryParams & AddressQueryParams & AssetQueryParams & EntryQueryParams & UserQueryParams;
export type MergedQueryParams = CommonQueryParams &
AddressQueryParams &
AssetQueryParams &
EntryQueryParams &
UserQueryParams;

// Common query methods shared by all element types, including allowed default methods
export interface CommonQueryBuilder {
Expand Down Expand Up @@ -109,13 +113,10 @@ export interface QueryBuilderMap {
}

// Generic implementation of the function
export function useCraftUrlBuilder<T extends ElementType>(
elementType: T,
): QueryBuilderMap[T] {

export function useCraftUrlBuilder<T extends ElementType>(elementType: T): QueryBuilderMap[T] {
const defaultParams: MergedQueryParams = {
elementType: 'entries'
}
elementType: 'entries',
};
let params: MergedQueryParams = defaultParams;
params.elementType = elementType;

Expand Down Expand Up @@ -148,10 +149,10 @@ export function useCraftUrlBuilder<T extends ElementType>(
buildBaseUrl(value) {
if (value === 'all') {
params.one = undefined;
params.all = '1'
params.all = '1';
} else {
params.one = '1';
params.all = undefined
params.all = undefined;
}

/* TODO: add more error handling */
Expand All @@ -162,10 +163,10 @@ export function useCraftUrlBuilder<T extends ElementType>(
);

const queryString = new URLSearchParams(queryParams).toString();
const previewToken = getPreviewToken()
return `/v1/api/customQuery?${queryString}${previewToken ? '&token=' + previewToken : ''}`
}
} as QueryBuilderMap[T];;
const previewToken = getPreviewToken();
return `/v1/api/customQuery?${queryString}${previewToken ? '&token=' + previewToken : ''}`;
},
} as QueryBuilderMap[T];

// Element-specific methods based on elementType
if (elementType === 'addresses') {
Expand Down Expand Up @@ -232,9 +233,7 @@ export function useCraftUrlBuilder<T extends ElementType>(
return this;
},
uri(value) {
params.uri = Array.isArray(value)
? value.filter((value) => value !== '').join('/')
: value;
params.uri = Array.isArray(value) ? value.filter((value) => value !== '').join('/') : value;
return this;
},
section(value) {
Expand Down
30 changes: 15 additions & 15 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import type { ElementType, ExecutionMethods } from '../index';

describe('useQueryBuilder Tests', () => {
describe('useQueryBuilder - CommonQueryBuilder', () => {
const arrOfExecutionTypes: ExecutionMethods[] = ['one', 'all']
const arrOfElementTypes: ElementType[] = ['addresses', 'assets', 'entries', 'users']
const arrOfExecutionTypes: ExecutionMethods[] = ['one', 'all'];
const arrOfElementTypes: ElementType[] = ['addresses', 'assets', 'entries', 'users'];

arrOfElementTypes.forEach(elementType => {
arrOfElementTypes.forEach((elementType) => {
const queryBuilder = useCraftUrlBuilder(elementType);
const baseQuery = queryBuilder
.id(1)
.limit(5)
.status('active')
.offset(2)
.orderBy('name')
.fields(['title', 'heroImage'])
arrOfExecutionTypes.forEach(executionType => {
.id(1)
.limit(5)
.status('active')
.offset(2)
.orderBy('name')
.fields(['title', 'heroImage']);

arrOfExecutionTypes.forEach((executionType) => {
it(`Should execute all commonQueryBuilder functions for ${elementType} with ${executionType}()`, async () => {
const queryUrlOne = baseQuery.buildBaseUrl(executionType);

expect(queryUrlOne).toContain(
`elementType=${elementType}&id=1&limit=5&status=active&offset=2&orderBy=name&fields=title%2CheroImage&${executionType}=1`,
);
Expand All @@ -44,7 +44,7 @@ describe('useQueryBuilder Tests', () => {
'elementType=addresses&addressLine1=123+Main+St&addressLine2=123&addressLine3=St.&locality=Springfield&fullName=John+Doe&one=1',
);
});
})
});

describe('useQueryBuilder - AssetQueryBuilder', () => {
it('Should execute all assetQueryBuilder functions', async () => {
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('useQueryBuilder Tests', () => {
// @ts-ignore: Is for testing
const nullQueryUrl = queryBuilder.slug(null).buildBaseUrl('one');
const emptyQueryUrl = queryBuilder.slug('').buildBaseUrl('one');
// @ts-ignore: Is for testing
// @ts-ignore: Is for testing
const undefinedQueryUrl = queryBuilder.slug(undefined).buildBaseUrl('one');

// Null value
Expand Down Expand Up @@ -148,4 +148,4 @@ describe('useQueryBuilder Tests', () => {
}).toThrowError('Unsupported element type: invalidType');
});
});
})
});
5 changes: 1 addition & 4 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@


export function getPreviewToken(): string|null {
export function getPreviewToken(): string | null {
if (typeof window === 'undefined') return null;
const windowUrl = window.location.search;
const urlParams = new URLSearchParams(windowUrl);

return urlParams.get('token');
}

0 comments on commit 23b3a17

Please sign in to comment.