Skip to content

Commit

Permalink
type guards
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyUnderStars committed Sep 29, 2023
1 parent 005727b commit 71a6450
Show file tree
Hide file tree
Showing 60 changed files with 1,349 additions and 1,048 deletions.
11 changes: 11 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"trailingComma": "all",
"tabWidth": 4,
"semi": true,
"arrowParens": "always",
"bracketSameLine": false,
"bracketSpacing": true,
"quoteProps": "as-needed",
"useTabs": true,
"singleQuote": false
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "activitypub-types",
"version": "1.0.4",
"version": "1.1.0",
"description": "ActivityPub Typescript types",
"main": "./dist/index.js",
"scripts": {
Expand Down
132 changes: 91 additions & 41 deletions src/fields/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import {
APCollection,
APOrderedCollection,
APObject,
APArticle,
APAudio,
APDocument,
APEvent,
APImage,
APNote,
APPage,
APPlace,
APProfile,
APRelationship,
APTombstone,
APVideo,
APLink,
APCollectionPage,
APOrderedCollectionPage,
APMention,
} from '../model-interfaces';
APArticle,
APAudio,
APCollection,
APCollectionPage,
APDocument,
APEvent,
APImage,
APLink,
APMention,
APNote,
APObject,
APOrderedCollection,
APOrderedCollectionPage,
APPage,
APPlace,
APProfile,
APRelationship,
APTombstone,
APVideo,
} from "../model-interfaces";

export type AnyCollection = APCollection | APOrderedCollection;
export type AnyAPObject = APObject | APArticle | APAudio | APDocument | APEvent | APImage | APNote | APPage | APPlace | APProfile | APRelationship | APTombstone | APVideo | AnyCollection;
export type AnyAPObject =
| APObject
| APArticle
| APAudio
| APDocument
| APEvent
| APImage
| APNote
| APPage
| APPlace
| APProfile
| APRelationship
| APTombstone
| APVideo
| AnyCollection;

export type DateTime = string | Date;
export type LanguageTag = string;
Expand Down Expand Up @@ -74,7 +88,14 @@ export type AltitudeField = number;
export type LatitudeField = number;
export type LongitudeField = number;
export type RadiusField = number;
export type UnitsField = 'cm' | 'feet' | 'inches' | 'km' | 'm' | 'miles' | string;
export type UnitsField =
| "cm"
| "feet"
| "inches"
| "km"
| "m"
| "miles"
| string;
export type ClosedField = string | AnyAPObject | APLink | DateTime | boolean;
export type FormerTypeField = string;
export type DeletedField = DateTime;
Expand All @@ -92,22 +113,51 @@ export type CollectionItemsField = string | APCollectionPage | APLink;
export type CollectionPagePartOfField = string | APCollection | APLink;
export type CollectionPageNextField = string | APCollection | APLink;
export type CollectionPagePrevField = string | APCollection | APLink;
export type OrderedCollectionCurrentField = string | APOrderedCollectionPage | APLink;
export type OrderedCollectionFirstField = string | APOrderedCollectionPage | APLink;
export type OrderedCollectionLastField = string | APOrderedCollectionPage | APLink;
export type OrderedCollectionItemsField = string | APOrderedCollectionPage | APLink;
export type OrderedCollectionPagePartOfField = string | APOrderedCollection | APLink;
export type OrderedCollectionPageNextField = string | APOrderedCollection | APLink;
export type OrderedCollectionPagePrevField = string | APOrderedCollection | APLink;
export type OrderedCollectionCurrentField =
| string
| APOrderedCollectionPage
| APLink;
export type OrderedCollectionFirstField =
| string
| APOrderedCollectionPage
| APLink;
export type OrderedCollectionLastField =
| string
| APOrderedCollectionPage
| APLink;
export type OrderedCollectionItemsField =
| string
| APOrderedCollectionPage
| APLink;
export type OrderedCollectionPagePartOfField =
| string
| APOrderedCollection
| APLink;
export type OrderedCollectionPageNextField =
| string
| APOrderedCollection
| APLink;
export type OrderedCollectionPagePrevField =
| string
| APOrderedCollection
| APLink;
export type StartIndexField = number;
export type SourceField = {
content: ContentField,
mediaType?: MediaType,
}
content: ContentField;
mediaType?: MediaType;
};
export type InboxField = string | APOrderedCollection | APLink;
export type OutboxField = string | APOrderedCollection | APLink;
export type FollowingField = string | APCollection | APOrderedCollection | APLink;
export type FollowersField = string | APCollection | APOrderedCollection | APLink;
export type FollowingField =
| string
| APCollection
| APOrderedCollection
| APLink;
export type FollowersField =
| string
| APCollection
| APOrderedCollection
| APLink;
export type LikedField = string | APCollection | APOrderedCollection | APLink;
export type LikesField = string | APCollection | APOrderedCollection | APLink;
export type SharesField = string | APCollection | APOrderedCollection | APLink;
Expand All @@ -121,10 +171,10 @@ export type ProvideClientKeyField = string;
export type SignClientKeyField = string;
export type SharedInboxField = string;
export type EndpointsField = {
proxyUrl?: ProxyUrlField;
oauthAuthorizationEndpoint?: OauthAuthorizationEndpointField;
oauthTokenEndpoint?: OauthTokenEndpointField;
provideClientKey?: ProvideClientKeyField;
signClientKey?: SignClientKeyField;
sharedInbox?: SharedInboxField;
}
proxyUrl?: ProxyUrlField;
oauthAuthorizationEndpoint?: OauthAuthorizationEndpointField;
oauthTokenEndpoint?: OauthTokenEndpointField;
provideClientKey?: ProvideClientKeyField;
signClientKey?: SignClientKeyField;
sharedInbox?: SharedInboxField;
};
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './fields';
export * from './model-interfaces';
export * from "./fields";
export * from "./model-interfaces";
10 changes: 8 additions & 2 deletions src/model-interfaces/activities/accept.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { APActivity } from './activity.interface';
import { APActivity } from "./activity.interface";

export type APAccept = APActivity
export type APAccept = APActivity & { type: "Accept" };

export const ActivityIsAccept = (
activity: APActivity,
): activity is APAccept => {
return activity.type == "Accept";
};
111 changes: 55 additions & 56 deletions src/model-interfaces/activities/activity.interface.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
import { APObject } from '../common/object.interface';
import { APObject } from "../common/object.interface";
import {
ActorField,
InstrumentField,
ObjectField,
OriginField,
ResultField,
TargetField
} from '../../fields';
ActorField,
InstrumentField,
ObjectField,
OriginField,
ResultField,
TargetField,
} from "../../fields";

export interface APActivity extends APObject {
/**
* Describes one or more entities that either performed or are expected to perform the activity.
* Any single activity can have multiple actors. The actor MAY be specified using an indirect Link.
*
* {@link https://www.w3.org/ns/activitystreams#actor Docs}
*/
actor?: ActorField | ActorField[];

/**
* Describes one or more entities that either performed or are expected to perform the activity.
* Any single activity can have multiple actors. The actor MAY be specified using an indirect Link.
*
* {@link https://www.w3.org/ns/activitystreams#actor Docs}
*/
actor?: ActorField | ActorField[];
/**
* When used within an Activity, describes the direct object of the activity.
* For instance, in the activity "John added a movie to his wishlist",
* the object of the activity is the movie added.
*
* {@link https://www.w3.org/ns/activitystreams#object Docs}
*/
object?: ObjectField | ObjectField[];

/**
* When used within an Activity, describes the direct object of the activity.
* For instance, in the activity "John added a movie to his wishlist",
* the object of the activity is the movie added.
*
* {@link https://www.w3.org/ns/activitystreams#object Docs}
*/
object?: ObjectField | ObjectField[];
/**
* Describes the indirect object, or target, of the activity. The precise meaning of the target is largely dependent
* on the type of action being described but will often be the object of the English preposition "to".
* For instance, in the activity "John added a movie to his wishlist", the target of the activity
* is John's wishlist. An activity can have more than one target.
*
* {@link https://www.w3.org/ns/activitystreams#target Docs}
*/
target?: TargetField | TargetField[];

/**
* Describes the indirect object, or target, of the activity. The precise meaning of the target is largely dependent
* on the type of action being described but will often be the object of the English preposition "to".
* For instance, in the activity "John added a movie to his wishlist", the target of the activity
* is John's wishlist. An activity can have more than one target.
*
* {@link https://www.w3.org/ns/activitystreams#target Docs}
*/
target?: TargetField | TargetField[];
/**
* Describes the result of the activity. For instance, if a particular action results
* in the creation of a new resource, the result property can be used to describe that new resource.
*
* {@link https://www.w3.org/ns/activitystreams#result Docs}
*/
result?: ResultField | ResultField[];

/**
* Describes the result of the activity. For instance, if a particular action results
* in the creation of a new resource, the result property can be used to describe that new resource.
*
* {@link https://www.w3.org/ns/activitystreams#result Docs}
*/
result?: ResultField | ResultField[];
/**
* Describes an indirect object of the activity from which the activity is directed.
* The precise meaning of the origin is the object of the English preposition "from".
* For instance, in the activity "John moved an item to List B from List A",
* the origin of the activity is "List A".
*
* {@link https://www.w3.org/ns/activitystreams#origin Docs}
*/
origin?: OriginField | OriginField[];

/**
* Describes an indirect object of the activity from which the activity is directed.
* The precise meaning of the origin is the object of the English preposition "from".
* For instance, in the activity "John moved an item to List B from List A",
* the origin of the activity is "List A".
*
* {@link https://www.w3.org/ns/activitystreams#origin Docs}
*/
origin?: OriginField | OriginField[];

/**
* Identifies one or more objects used (or to be used)
* in the completion of an Activity.
*
* {@link https://www.w3.org/ns/activitystreams#instrument Docs}
*/
instrument?: InstrumentField | InstrumentField[];
/**
* Identifies one or more objects used (or to be used)
* in the completion of an Activity.
*
* {@link https://www.w3.org/ns/activitystreams#instrument Docs}
*/
instrument?: InstrumentField | InstrumentField[];
}
8 changes: 6 additions & 2 deletions src/model-interfaces/activities/add.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { APActivity } from './activity.interface';
import { APActivity } from "./activity.interface";

export type APAdd = APActivity
export type APAdd = APActivity & { type: "Add" };

export const ActivityIsAdd = (activity: APActivity): activity is APAdd => {
return activity.type == "Add";
};
10 changes: 8 additions & 2 deletions src/model-interfaces/activities/announce.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { APActivity } from './activity.interface';
import { APActivity } from "./activity.interface";

export type APAnnounce = APActivity
export type APAnnounce = APActivity & { type: "Announce" };

export const ActivityIsAnnounce = (
activity: APActivity,
): activity is APAnnounce => {
return activity.type == "Announce";
};
10 changes: 8 additions & 2 deletions src/model-interfaces/activities/arrive.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { APIntransitiveActivity } from './intransitive-activity.interface';
import { APIntransitiveActivity } from "./intransitive-activity.interface";

export type APArrive = APIntransitiveActivity
export type APArrive = APIntransitiveActivity & { type: "Arrive" };

export const ActivityIsArrive = (
activity: APIntransitiveActivity,
): activity is APArrive => {
return activity.type == "Arrive";
};
9 changes: 7 additions & 2 deletions src/model-interfaces/activities/block.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { APIgnore } from './ignore.interface';
import { APActivity } from "./activity.interface";
import { APIgnore } from "./ignore.interface";

export type APBlock = APIgnore
export type APBlock = APIgnore & { type: "Block" };

export const ActivityIsBlock = (activity: APActivity): activity is APBlock => {
return activity.type == "Block";
};
10 changes: 8 additions & 2 deletions src/model-interfaces/activities/create.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { APActivity } from './activity.interface';
import { APActivity } from "./activity.interface";

export type APCreate = APActivity
export type APCreate = APActivity & { type: "Create" };

export const ActivityIsCreate = (
activity: APActivity,
): activity is APCreate => {
return activity.type == "Create";
};
10 changes: 8 additions & 2 deletions src/model-interfaces/activities/delete.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { APActivity } from './activity.interface';
import { APActivity } from "./activity.interface";

export type APDelete = APActivity
export type APDelete = APActivity & { type: "Delete" };

export const ActivityIsDelete = (
activity: APActivity,
): activity is APDelete => {
return activity.type == "Delete";
};
Loading

0 comments on commit 71a6450

Please sign in to comment.