-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nanoid code replacement, schema code replacement fixes.
- Loading branch information
Showing
4 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Borrowed from https://github.com/ai/nanoid/blob/3.0.2/non-secure/index.js | ||
// This alphabet uses `A-Za-z0-9_-` symbols. A genetic algorithm helped | ||
// optimize the gzip compression for this alphabet. | ||
const urlAlphabet = | ||
'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'; | ||
|
||
/** | ||
* | ||
* @public | ||
*/ | ||
export function nanoid(size = 21) { | ||
let id = ''; | ||
// A compact alternative for `for (var i = 0; i < step; i++)`. | ||
let i = size; | ||
// eslint-disable-next-line no-plusplus | ||
while (i--) { | ||
// `| 0` is more compact and faster than `Math.floor()`. | ||
// eslint-disable-next-line no-bitwise | ||
id += urlAlphabet[(Math.random() * 64) | 0]; | ||
} | ||
return id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { | ||
LATEST_SCHEMA_QUERY_KEY, | ||
SCHEMA_QUERY_KEY, | ||
SCHEMAS_VERSION_QUERY_KEY, | ||
} from 'lib/queries'; | ||
import { | ||
GetAllVersionsBySubjectRequest, | ||
GetLatestSchemaRequest, | ||
GetSchemasRequest, | ||
NewSchemaSubject, | ||
SchemaSubject, | ||
UpdateGlobalSchemaCompatibilityLevelRequest, | ||
UpdateSchemaCompatibilityLevelRequest, | ||
} from 'generated-sources'; | ||
import { schemasApiClient } from 'lib/api'; | ||
import { ClusterName } from 'redux/interfaces'; | ||
|
||
export function useGetLatestSchemas(param: GetLatestSchemaRequest) { | ||
return useQuery({ | ||
queryKey: [ | ||
SCHEMA_QUERY_KEY, | ||
LATEST_SCHEMA_QUERY_KEY, | ||
param.clusterName, | ||
param.subject, | ||
], | ||
queryFn: () => schemasApiClient.getLatestSchema(param), | ||
}); | ||
} | ||
|
||
export function useGetSchemas({ | ||
clusterName, | ||
page, | ||
perPage, | ||
search, | ||
}: GetSchemasRequest) { | ||
return useQuery({ | ||
queryKey: [SCHEMA_QUERY_KEY, clusterName, page, perPage, search], | ||
queryFn: () => | ||
schemasApiClient.getSchemas({ | ||
clusterName, | ||
page, | ||
perPage, | ||
search: search || undefined, | ||
}), | ||
}); | ||
} | ||
|
||
export function useGetSchemasVersions({ | ||
clusterName, | ||
subject, | ||
}: GetAllVersionsBySubjectRequest) { | ||
return useQuery({ | ||
queryKey: [SCHEMAS_VERSION_QUERY_KEY, clusterName, subject], | ||
queryFn: () => | ||
schemasApiClient.getAllVersionsBySubject({ | ||
clusterName, | ||
subject, | ||
}), | ||
}); | ||
} | ||
|
||
export function useCreateSchema(clusterName: ClusterName) { | ||
const queryClient = useQueryClient(); | ||
return useMutation<SchemaSubject, void, NewSchemaSubject>({ | ||
mutationFn: ({ subject, schema, schemaType }) => | ||
schemasApiClient.createNewSchema({ | ||
clusterName, | ||
newSchemaSubject: { subject, schema, schemaType }, | ||
}), | ||
onSuccess: () => { | ||
return queryClient.invalidateQueries(); | ||
}, | ||
}); | ||
} | ||
|
||
export function useUpdateSchemaCompatibilityLayer(clusterName: ClusterName) { | ||
const queryClient = useQueryClient(); | ||
return useMutation< | ||
void, | ||
void, | ||
Omit<UpdateSchemaCompatibilityLevelRequest, 'clusterName'> | ||
>({ | ||
mutationFn: ({ subject, compatibilityLevel }) => | ||
schemasApiClient.updateSchemaCompatibilityLevel({ | ||
clusterName, | ||
subject, | ||
compatibilityLevel, | ||
}), | ||
onSuccess: () => { | ||
return queryClient.invalidateQueries(); | ||
}, | ||
}); | ||
} | ||
|
||
export function useUpdateGlobalSchemaCompatibilityLevel( | ||
clusterName: ClusterName | ||
) { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation< | ||
void, | ||
void, | ||
Omit<UpdateGlobalSchemaCompatibilityLevelRequest, 'clusterName'> | ||
>({ | ||
mutationFn: ({ compatibilityLevel }) => | ||
schemasApiClient.updateGlobalSchemaCompatibilityLevel({ | ||
clusterName, | ||
compatibilityLevel, | ||
}), | ||
onSuccess: () => { | ||
return queryClient.invalidateQueries(); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// TODO all the other query key should be transferred here | ||
|
||
// SCHEMAS | ||
export const SCHEMA_QUERY_KEY = 'schemas'; | ||
export const LATEST_SCHEMA_QUERY_KEY = 'latest_schemas'; | ||
export const SCHEMAS_VERSION_QUERY_KEY = 'schemas_version'; |