Just a quick explanation of a design that I use in Dwarf Fortress for caravans.  It consists of a 5-tile wide paved road inside of a 7-tile wide walled area in the shape of a mushroom / mirrored u-bend.  Caravan wagons need at least a 3-tile wide entrance, but you should allow 7-tiles so that simultaneous incoming / outgoing caravans don't pile up in a traffic jam.

Enemies in Dwarf Fortress will almost always make a beeline for your dinining room / trade depot area and will take the shortest route.  Because the caravan entrance is longer then getting to the side entrance, very few enemies will take the untrapped and mostly unguarded caravan route.  Still, you should have at least one drawbridge / retracting bridge that you can use to close off the caravan entrance.