aboutsummaryrefslogtreecommitdiff
path: root/src/lib/stores
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2022-05-31 01:09:58 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2022-05-31 01:09:58 +0200
commit6ccc6f60fc85e665c8a07a169efbe8d09c9d9e8e (patch)
tree87c397af49b7820b5fb2c0fa3037be2ac4a587e8 /src/lib/stores
parent852ee620f0a2f6a83cf83eba860ca951b66bb7e2 (diff)
Lint, rm unused code
Diffstat (limited to 'src/lib/stores')
-rw-r--r--src/lib/stores/forums.ts21
-rw-r--r--src/lib/stores/posts.ts29
-rw-r--r--src/lib/stores/response_builder.ts40
-rw-r--r--src/lib/stores/supabase.ts50
-rw-r--r--src/lib/stores/tags.test.ts295
-rw-r--r--src/lib/stores/tags.ts13
-rw-r--r--src/lib/stores/topics.ts43
-rw-r--r--src/lib/stores/users.ts13
8 files changed, 100 insertions, 404 deletions
diff --git a/src/lib/stores/forums.ts b/src/lib/stores/forums.ts
index 40a3f2c..0bf41f6 100644
--- a/src/lib/stores/forums.ts
+++ b/src/lib/stores/forums.ts
@@ -1,4 +1,4 @@
-import { createClient } from '@supabase/supabase-js'
+import { createClient } from '@supabase/supabase-js';
import { single, collection } from './supabase';
import { supabase } from '$lib/config/config';
@@ -6,13 +6,20 @@ import type { Forum } from '$lib/data/types';
const client = createClient(supabase.url, supabase.key);
-export const forum = (id: string, withTopics = false) => single<Forum>(client
- .from('forums')
- .select(withTopics ? `*,
+export const forum = (id: string, withTopics = false) =>
+ single<Forum>(
+ client
+ .from('forums')
+ .select(
+ withTopics
+ ? `*,
topics (
*
)
- `: '*' )
- .eq('id', id),
- null);
+ `
+ : '*'
+ )
+ .eq('id', id),
+ null
+ );
export const forums = collection<Forum>(client.from('forums').select('*'), []);
diff --git a/src/lib/stores/posts.ts b/src/lib/stores/posts.ts
index f592dca..388eed3 100644
--- a/src/lib/stores/posts.ts
+++ b/src/lib/stores/posts.ts
@@ -1,23 +1,24 @@
-import { createClient } from '@supabase/supabase-js'
-import { single, collection } from './supabase';
+import { createClient } from '@supabase/supabase-js';
+import { single } from './supabase';
import { supabase } from '$lib/config/config';
import type { Post } from '$lib/data/types';
const client = createClient(supabase.url, supabase.key);
-export const post = (id: string, withTopic = false) => single<Post>(client
- .from('posts')
- .select(withTopic ? `*,
+export const post = (id: string, withTopic = false) =>
+ single<Post>(
+ client
+ .from('posts')
+ .select(
+ withTopic
+ ? `*,
topic:topic_id (
*
)
- `: '*' )
- .eq('id', id),
- null);
-export const postsForTopic = (id: string) => collection<Post>(client
- .from('posts')
- .select('*')
- .eq('topic_id', id)
- .order('created_at', { ascending: true }),
- []);
+ `
+ : '*'
+ )
+ .eq('id', id),
+ null
+ );
diff --git a/src/lib/stores/response_builder.ts b/src/lib/stores/response_builder.ts
index 1e12243..91e8d0f 100644
--- a/src/lib/stores/response_builder.ts
+++ b/src/lib/stores/response_builder.ts
@@ -3,8 +3,8 @@
*/
interface AnyError {
- message: string
-};
+ message: string;
+}
export type StoreState<Type> = {
loading: boolean;
@@ -12,26 +12,26 @@ export type StoreState<Type> = {
error: AnyError | void;
};
-export function initialResponse<T> (initialState: T): StoreState<T> {
- return {
- loading: true,
- data: initialState,
- error: undefined
- };
+export function initialResponse<T>(initialState: T): StoreState<T> {
+ return {
+ loading: true,
+ data: initialState,
+ error: undefined
+ };
}
-export function errorResponse<T> (error: AnyError): StoreState<T> {
- return {
- loading: false,
- data: undefined,
- error
- };
+export function errorResponse<T>(error: AnyError): StoreState<T> {
+ return {
+ loading: false,
+ data: undefined,
+ error
+ };
}
-export function response<T> (data: T): StoreState<T> {
- return {
- loading: false,
- data,
- error: undefined
- };
+export function response<T>(data: T): StoreState<T> {
+ return {
+ loading: false,
+ data,
+ error: undefined
+ };
}
diff --git a/src/lib/stores/supabase.ts b/src/lib/stores/supabase.ts
index 804141f..5bcd941 100644
--- a/src/lib/stores/supabase.ts
+++ b/src/lib/stores/supabase.ts
@@ -5,34 +5,36 @@ import type { Readable } from 'svelte/store';
import type { PostgrestFilterBuilder } from '@supabase/postgrest-js';
import type { StoreState } from './response_builder';
-export function collection<T> (query: PostgrestFilterBuilder<T>, initialValue: T[]): Readable<StoreState<T[]>> {
+export function collection<T>(
+ query: PostgrestFilterBuilder<T>,
+ initialValue: T[]
+): Readable<StoreState<T[]>> {
+ return readable(initialResponse<T[]>(initialValue), (set) => {
+ (async function () {
+ const { data, error } = await query;
- return readable(initialResponse<T[]>(initialValue), (set) => {
+ if (error) {
+ return set(errorResponse<T[]>(error));
+ }
- (async function() {
- const { data, error } = await query;
-
- if (error) {
- return set(errorResponse<T[]>(error));
- }
-
- set(response<T[]>(data));
- })()
- });
+ set(response<T[]>(data));
+ })();
+ });
}
-export function single<T> (query: PostgrestFilterBuilder<T>, initialValue: T): Readable<StoreState<T>> {
-
- return readable(initialResponse<T>(initialValue), (set) => {
-
- (async function() {
- const { data, error } = await query.single();
+export function single<T>(
+ query: PostgrestFilterBuilder<T>,
+ initialValue: T
+): Readable<StoreState<T>> {
+ return readable(initialResponse<T>(initialValue), (set) => {
+ (async function () {
+ const { data, error } = await query.single();
- if (error) {
- return set(errorResponse<T>(error));
- }
+ if (error) {
+ return set(errorResponse<T>(error));
+ }
- set(response<T>(data));
- })()
- });
+ set(response<T>(data));
+ })();
+ });
}
diff --git a/src/lib/stores/tags.test.ts b/src/lib/stores/tags.test.ts
deleted file mode 100644
index 8c9effe..0000000
--- a/src/lib/stores/tags.test.ts
+++ /dev/null
@@ -1,295 +0,0 @@
-import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
-import { resolve } from 'path';
-
-import { resolveAfter } from '$lib/utils/resolve_after';
-
-const { eachLike, like } = Matchers;
-
-jest.mock('$lib/config/config.ts');
-
-import { getTag } from './tags';
-
-const internals = {
- provider: null
-};
-
-describe('Tags store pact', () => {
- beforeAll(async () => {
- internals.provider = new Pact({
- port: 1234,
- dir: resolve(process.cwd(), 'pacts'),
- consumer: 'ForumClient',
- provider: 'ForumServer',
- pactfileWriteMode: 'update'
- });
-
- await internals.provider.setup();
- });
-
- afterEach(() => internals.provider.verify());
- afterAll(() => internals.provider.finalize());
-
- describe("When there's data", () => {
- describe('GetTag', () => {
- beforeAll(async () => {
- const tagQuery = new GraphQLInteraction()
- .given("there's data")
- .uponReceiving('a request to get a single tag')
- .withRequest({
- path: '/graphql',
- method: 'POST'
- })
- .withOperation('GetTag')
- .withQuery(
- `query GetTag($id: ID!) {
- tag(id: $id) {
- id
- topics {
- id
- title
- updated_at
- ttl
- __typename
- }
- __typename
- }
- }`
- )
- .withVariables({
- id: 'pineapple'
- })
- .willRespondWith({
- status: 200,
- headers: {
- 'Content-Type': 'application/json; charset=utf-8'
- },
- body: {
- data: {
- tag: {
- id: like('pineapple'),
- topics: eachLike({
- id: like('cd038ae7-e8b4-4e38-9543-3d697e69ac34'),
- title: like('This topic is about pineapples'),
- updated_at: like(1619978944077),
- ttl: like(3555)
- })
- }
- }
- }
- });
- return await internals.provider.addInteraction(tagQuery);
- });
-
- test('it returns the tag', async () => {
- const tag = getTag('pineapple');
- const { counter, promise: resolveAfterTwo } = resolveAfter(2);
- let response = null;
- tag.subscribe((tagValue) => {
- response = tagValue;
- counter();
- });
- expect(response.data).toBe(null);
- expect(response.loading).toBe(true);
- expect(response.error).toBe(undefined);
- await resolveAfterTwo;
- expect(response.data).toEqual({
- id: 'pineapple',
- topics: [
- {
- id: 'cd038ae7-e8b4-4e38-9543-3d697e69ac34',
- title: 'This topic is about pineapples',
- updated_at: 1619978944077,
- ttl: 3555
- }
- ]
- });
- expect(response.loading).toBe(false);
- expect(response.error).toBe(undefined);
- });
- });
- });
-
- describe("When there's no data", () => {
- describe('GetTag', () => {
- beforeAll(async () => {
- const tagQuery = new GraphQLInteraction()
- .given("there's no data")
- .uponReceiving('a request to get a single tag')
- .withRequest({
- path: '/graphql',
- method: 'POST'
- })
- .withOperation('GetTag')
- .withQuery(
- `query GetTag($id: ID!) {
- tag(id: $id) {
- id
- topics {
- id
- title
- updated_at
- ttl
- __typename
- }
- __typename
- }
- }`
- )
- .withVariables({
- id: 'pineapple'
- })
- .willRespondWith({
- status: 200,
- headers: {
- 'Content-Type': 'application/json; charset=utf-8'
- },
- body: {
- data: {
- tag: null
- }
- }
- });
- return await internals.provider.addInteraction(tagQuery);
- });
-
- test('it returns the tag', async () => {
- const tag = getTag('pineapple');
- const { counter, promise: resolveAfterTwo } = resolveAfter(2);
- let response = null;
- tag.subscribe((tagValue) => {
- response = tagValue;
- counter();
- });
- expect(response.data).toBe(null);
- expect(response.loading).toBe(true);
- expect(response.error).toBe(undefined);
- await resolveAfterTwo;
- expect(response.data).toBe(null);
- expect(response.loading).toBe(false);
- expect(response.error).toBe(undefined);
- });
- });
- });
-
- describe("When there's a server error", () => {
- describe('GetTag', () => {
- beforeAll(async () => {
- const tagQuery = new GraphQLInteraction()
- .given("there's a server error")
- .uponReceiving('a request to get a single tag')
- .withRequest({
- path: '/graphql',
- method: 'POST'
- })
- .withOperation('GetTag')
- .withQuery(
- `query GetTag($id: ID!) {
- tag(id: $id) {
- id
- topics {
- id
- title
- updated_at
- ttl
- __typename
- }
- __typename
- }
- }`
- )
- .withVariables({
- id: 'pineapple'
- })
- .willRespondWith({
- status: 500
- });
- return await internals.provider.addInteraction(tagQuery);
- });
-
- test('it returns the error', async () => {
- const tag = getTag('pineapple');
- const { counter, promise: resolveAfterTwo } = resolveAfter(2);
- let response = null;
- tag.subscribe((tagValue) => {
- response = tagValue;
- counter();
- });
- expect(response.data).toBe(null);
- expect(response.loading).toBe(true);
- expect(response.error).toBe(undefined);
- await resolveAfterTwo;
- expect(response.data).toBe(null);
- expect(response.loading).toBe(false);
- expect(response.error).toBeInstanceOf(Error);
- });
- });
- });
-
- describe("When there's an error in the response", () => {
- describe('GetTag', () => {
- beforeAll(async () => {
- const tagQuery = new GraphQLInteraction()
- .given("there's an error in the response")
- .uponReceiving('a request to get a single tag')
- .withRequest({
- path: '/graphql',
- method: 'POST'
- })
- .withOperation('GetTag')
- .withQuery(
- `query GetTag($id: ID!) {
- tag(id: $id) {
- id
- topics {
- id
- title
- updated_at
- ttl
- __typename
- }
- __typename
- }
- }`
- )
- .withVariables({
- id: 'pineapple'
- })
- .willRespondWith({
- status: 200,
- headers: {
- 'Content-Type': 'application/json; charset=utf-8'
- },
- body: {
- errors: eachLike({
- message: like('An error occurred when fetching the tag')
- })
- }
- });
- return await internals.provider.addInteraction(tagQuery);
- });
-
- test('it returns the error', async () => {
- const tag = getTag('pineapple');
- const { counter, promise: resolveAfterTwo } = resolveAfter(2);
- let response = null;
- tag.subscribe((tagValue) => {
- response = tagValue;
- counter();
- });
- expect(response.data).toBe(null);
- expect(response.loading).toBe(true);
- expect(response.error).toBe(undefined);
- await resolveAfterTwo;
- expect(response.data).toBe(null);
- expect(response.loading).toBe(false);
- expect(response.error.graphQLErrors).toEqual(
- expect.arrayContaining([
- {
- message: 'An error occurred when fetching the tag'
- }
- ])
- );
- });
- });
- });
-});
diff --git a/src/lib/stores/tags.ts b/src/lib/stores/tags.ts
deleted file mode 100644
index 0b528e8..0000000
--- a/src/lib/stores/tags.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { createClient } from '@supabase/supabase-js'
-import { collection } from './supabase';
-import { supabase } from '$lib/config/config';
-
-import type { Tag } from '$lib/data/types';
-
-const client = createClient(supabase.url, supabase.key);
-
-export const tagsForTopic = (id: string) => collection<Tag>(client
- .from('topic_tags')
- .select('*')
- .eq('topic_id', id),
- []);
diff --git a/src/lib/stores/topics.ts b/src/lib/stores/topics.ts
index 04c1e9e..1842a4d 100644
--- a/src/lib/stores/topics.ts
+++ b/src/lib/stores/topics.ts
@@ -1,4 +1,4 @@
-import { createClient } from '@supabase/supabase-js'
+import { createClient } from '@supabase/supabase-js';
import { single, collection } from './supabase';
import { supabase } from '$lib/config/config';
@@ -6,27 +6,34 @@ import type { Topic } from '$lib/data/types';
const client = createClient(supabase.url, supabase.key);
-export const topic = (id: string, withPosts = false) => single<Topic>(client
- .from('topics')
- .select(withPosts ? `*,
+export const topic = (id: string, withPosts = false) =>
+ single<Topic>(
+ client
+ .from('topics')
+ .select(
+ withPosts
+ ? `*,
forum: forums (*),
tags: topic_tags (*),
posts (
*,
author:author_id (*)
)
- `: '*' )
- .eq('id', id),
- null);
-export const topicsForForum = (id: string) => collection<Topic>(client
- .from('topics')
- .select('*')
- .eq('forum_id', id),
- []);
-export const topicsForTag = (id: string) => collection<Topic>(client
- .from('topics')
- .select(`
+ `
+ : '*'
+ )
+ .eq('id', id),
+ null
+ );
+export const topicsForTag = (id: string) =>
+ collection<Topic>(
+ client
+ .from('topics')
+ .select(
+ `
*,tags!inner(*)
- `)
- .eq('tags.tag', id),
- []);
+ `
+ )
+ .eq('tags.tag', id),
+ []
+ );
diff --git a/src/lib/stores/users.ts b/src/lib/stores/users.ts
deleted file mode 100644
index 2a56cb5..0000000
--- a/src/lib/stores/users.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { createClient } from '@supabase/supabase-js'
-import { single } from './supabase';
-import { supabase } from '$lib/config/config';
-
-import type { User } from '$lib/data/types';
-
-const client = createClient(supabase.url, supabase.key);
-
-export const user = (id: string) => single<User>(client
- .from('users')
- .select('*')
- .eq('id', id),
- null);